realgam3 / requests-raw

Use requests to send HTTP raw sockets (To Test RFC Compliance)
Apache License 2.0
20 stars 1 forks source link

A exception happen since "method" param is None #1

Closed Ovi3 closed 3 years ago

Ovi3 commented 4 years ago

Use Python 3 to reproduce this problem:

import requests_raw

if __name__ == "__main__":
    data = b"""
POST /m_graphql HTTP/1.1
Host: live.kuaishou.com
Connection: close
Content-Length: 418
accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4157.69 Safari/537.36
content-type: application/json
Origin: https://live.kuaishou.com
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://live.kuaishou.com/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: kuaishou.live.bfb1s=7206d814e5c089a58c910ed8bf52ace5; clientid=3; did=web_86c69fc7a60c69f314ea3cf176531dd9; client_key=65890b29

{"operationName":"categoryListQuery","variables":{"type":"panel","categoryCardList":true},"query":"query categoryListQuery($limit: Int, $type: String, $categoryCardList: Boolean) {\\n  categoryList(limit: $limit, type: $type, categoryCardList: $categoryCardList) {\\n    categoryId\\n    categoryAbbr\\n    name\\n    category\\n    title\\n    src\\n    shortName\\n    gameDescription\\n    subList\\n    __typename\\n  }\\n}\\n"}
    """.strip().replace(b"\n", b"\r\n")

    requests_raw.raw("https://live.kuaishou.com/m_graphql", data=data)

run the code, raise the exception:

Traceback (most recent call last):
  File "F:/python/mytest/mytest.py", line 36, in <module>
    requests_raw.raw("https://live.kuaishou.com/m_graphql", data=data)
  File "F:\python\mytest\venv\lib\site-packages\requests_raw\api.py", line 7, in raw
    return session.request(__title__, url, data=data, **kwargs)
  File "F:\python\mytest\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "F:\python\mytest\venv\lib\site-packages\requests\sessions.py", line 685, in send
    r.content
  File "F:\python\mytest\venv\lib\site-packages\requests\models.py", line 829, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "F:\python\mytest\venv\lib\site-packages\requests\models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "F:\python\mytest\venv\lib\site-packages\urllib3\response.py", line 571, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "F:\python\mytest\venv\lib\site-packages\urllib3\response.py", line 753, in read_chunked
    if self._original_response and is_response_to_head(self._original_response):
  File "F:\python\mytest\venv\lib\site-packages\urllib3\util\response.py", line 86, in is_response_to_head
    return method.upper() == "HEAD"
AttributeError: 'NoneType' object has no attribute 'upper'

In /Lib/site-packages/urllib3/response.py:251:

        if "chunked" in encodings:
            self.chunked = True

This IF statement is true, and set the self.chunked = true. let the programe run into where the exception is raised

realgam3 commented 4 years ago

Thank you for the information, Issue reproduced, I will fix it as soon as possible :)

I think I found the root cause: You can remove: "Accept-Encoding: gzip, deflate" for now and it should work fine...

Phoenix1112 commented 4 years ago

@realgam3 i have same error.. but i am not using Accept-Encoding????

res = requests_raw.raw(url="https://www.google.com", data=b"GET /?ani=asas HTTP/1.1\r\nHost: www.google.com\r\nUser-Agent: Mozilla Firefox 55/66\r\n\r\n")

www.google.com\r\nUser-Agent: Mozilla Firefox 55/66\r\n\r\n")
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    res = requests_raw.raw(url="https://www.google.com",data="GET /?ani=<cyberthereaper> HTTP/1.1\r\nHost: www.google.com\r\nUser-Agent: Mozilla Firefox 55/66\r\n\r\n")
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests_raw\api.py", line 7, in raw
    return session.request(__title__, url, data=data, **kwargs)
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\sessions.py", line 697, in send
    r.content
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\models.py", line 831, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\models.py", line 753, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 572, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 754, in read_chunked
    if self._original_response and is_response_to_head(self._original_response):
  File "C:\Users\Levent\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\util\response.py", line 107, in is_response_to_head
    return method.upper() == "HEAD"
AttributeError: 'NoneType' object has no attribute 'upper'
Phoenix1112 commented 4 years ago

@realgam3 Will you write an answer? I urgently need this module.

realgam3 commented 4 years ago

Tomorrow I will try to fix it 😊

On Sun, 29 Nov 2020 at 0:19 Phoenix1112 notifications@github.com wrote:

@realgam3 https://github.com/realgam3 Will you write an answer? I urgently need this module.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/realgam3/requests-raw/issues/1#issuecomment-735297849, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKOSGFS3JM5IIHY5QYOXXTSSFZP5ANCNFSM4P43MGMA .

Phoenix1112 commented 4 years ago

@realgam3 thanks❤❤ i think problem is Transfer-Encoding: chunked

realgam3 commented 3 years ago

It's not an easy issue because its in the response and I'm not sure why it has no method.... but I will give you my workaround for this issue until I fix it :)

from requests.packages.urllib3 import response

# Monkey Patch
def _is_response_to_head(response):
    """

    Checks whether the request of a response has been a HEAD-request.
    Handles the quirks of AppEngine.

    :param conn:
    :type conn: :class:`httplib.HTTPResponse`
    """
    # FIXME: Can we do this somehow without accessing private httplib _method?
    method = response._method or ""
    if isinstance(method, int):  # Platform-specific: Appengine
        return method == 3
    return method.upper() == "HEAD"

response.is_response_to_head = _is_response_to_head

if __name__ == "__main__":
    data = b"""
POST /m_graphql HTTP/1.1
Host: live.kuaishou.com
Connection: close
Content-Length: 418
accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4157.69 Safari/537.36
content-type: application/json
Origin: https://live.kuaishou.com
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://live.kuaishou.com/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: kuaishou.live.bfb1s=7206d814e5c089a58c910ed8bf52ace5; clientid=3; did=web_86c69fc7a60c69f314ea3cf176531dd9; client_key=65890b29

{"operationName":"categoryListQuery","variables":{"type":"panel","categoryCardList":true},"query":"query categoryListQuery($limit: Int, $type: String, $categoryCardList: Boolean) {\\n  categoryList(limit: $limit, type: $type, categoryCardList: $categoryCardList) {\\n    categoryId\\n    categoryAbbr\\n    name\\n    category\\n    title\\n    src\\n    shortName\\n    gameDescription\\n    subList\\n    __typename\\n  }\\n}\\n"}
    """.strip().replace(b"\n", b"\r\n")

    print(requests_raw.raw("https://live.kuaishou.com/m_graphql", data=data).text)
Phoenix1112 commented 3 years ago

@realgam3 thanks <3

realgam3 commented 3 years ago

Fixed on 1.0.0