Skip to content

bug: request body rewrite cannot automatically modify content-length request header. #154

Description

@zxyao145

Issue description

I am trying to rewrite the request body and response body , and the RequestFilter method code is roughly as follows:

origBody := []byte("{\"keyword\": \"xxxxx\"}")
log.Infof("request get Content-Length 1: %s", r.Header().Get("Content-Length"))
r.SetBody(origBody)
log.Infof("request get Content-Length 2: %s", r.Header().Get("Content-Length"))
r.Header().Set("Content-Length", strconv.Itoa(len(origBody)))
log.Infof("request get Content-Length 3: %s", r.Header().Get("Content-Length"))

and the log as follows:

request get Content-Length 1: 158
request get Content-Length 2: 158
request get Content-Length 3: 20

If I don't manually set the Content-Length, I will find that ResponseFilter method of the same plugin cannot be triggered and a timeout exception will occur.

Traceback (most recent call last):
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 462, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 1344, in getresponse
    response.begin()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\http\client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

If this is by design, please update the documentation and the demo.

Environment

  • APISIX Go Plugin Runner's version:
    github.com/apache/apisix-go-plugin-runner v0.5.1-0.20231128010119-a265bcd63ca1
  • APISIX version:
    3.6.0
  • Go version:
    1.19
  • OS (cmd: uname -a):
    debian:bullseye-slim

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions