Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix httpretrieve #151

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions httpretrieve.r2py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ def httpretrieve_open(url, querydata=None, postdata=None,\
both postdata and querydata are omitted, there is no query
string sent in the request.

For both querydata and postdata, strings are sent *unmodified*.
This means you probably should encode them first, with
urllib_quote().
Per default, we use
`Content-Encoding: application/x-www-form-urlencoded`
for querydata and postdata. However, we do not MIME-encode the
data; the caller must take care of that (using `urllib_quote`).
If you encode querydata/postdata differently, also supply a
`httpheaders` dict, with the key `Content-Encoding` mapping to
the MIME type you use.
httpheaders (optional):
A dictionary of supplemental HTTP request headers to add to the
request.
Expand Down Expand Up @@ -504,21 +508,27 @@ def _httpretrieve_build_request(host, port, path, querydata, postdata, \
# Sanity checks:
if path == "":
raise ValueError("Invalid path -- empty string.")
if postdata is not None and type(postdata) not in (str, dict):
raise TypeError("Postdata should be a dict of form-data or a string")
if querydata is not None and type(querydata) not in (str, dict):
raise TypeError("Querydata should be a dict of form-data or a string")
if httpheaders is not None and type(httpheaders) is not dict:
raise TypeError("Expected HTTP headers as a dictionary.")

# Type-conversions:
if type(querydata) is dict:
querydata = urllib_quote_parameters(querydata)
elif querydata is None:
querydata = ""

if type(postdata) is dict:
postdata = urllib_quote_parameters(postdata)
if postdata is not None and type(postdata) is not str:
raise TypeError("postdata should be a MIME-encoded string")

# Initialize querydata if not given, ...
querydata = querydata or ""

# ... and ensure it is a str
if type(querydata) is not str:
raise TypeError("querydata should be a MIME-encoded string")

# Initialize the HTTP headers dict if not given, ...
httpheaders = httpheaders or {}

# ... and ensure it is a dict
if type(httpheaders) is not dict:
raise TypeError("httpheaders must be a dict")

# If we have query or post data, set a default encoding if not given
if querydata or postdata and "Content-Type" not in httpheaders:
httpheaders['Content-Type'] = 'application/x-www-form-urlencoded'


# Default to GET, unless the caller specifies a message body to send.
methodstr = "GET"
Expand All @@ -538,24 +548,23 @@ def _httpretrieve_build_request(host, port, path, querydata, postdata, \
# there is no proxy; send normal http request
requeststr = methodstr + ' ' + path + resourcestr + ' HTTP/1.0\r\n'

if httpheaders is not None:
# Most servers require a 'Host' header for normal functionality
# (especially in the case of multiple domains being hosted on a
# single server).
if "Host" not in httpheaders:
requeststr += "Host: " + host + ':' + str(port) + "\r\n"
# Most servers require a 'Host' header for normal functionality
# (especially in the case of multiple domains being hosted on a
# single server).
if "Host" not in httpheaders:
requeststr += "Host: " + host + ':' + str(port) + "\r\n"

for key, val in httpheaders.items():
requeststr += key + ": " + val + '\r\n'
for key, val in httpheaders.items():
requeststr += key + ": " + val + '\r\n'

# Affix post-data related headers and content:
# Add post-data related headers and content:
if methodstr == "POST":
requeststr += 'Content-Length: ' + str(len(postdata)) + '\r\n'

# The empty line terminates HTTP headers.
requeststr += '\r\n'

# If we're a POST request, affix any requested data to the message body.
# If we're a POST request, add any requested data to the message body.
if methodstr == "POST":
requeststr += postdata

Expand Down
5 changes: 0 additions & 5 deletions librepysocket.r2py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ def get_connports(localip=None):
<Returns>
A list of valid connports for localport
"""
# Check if the IP is valid
if localip is not None and not _is_valid_ip_address(localip):
raise RepyArgumentError("Invalid IP address specified!")

# Resolve the IP
if localip is None:
Expand Down Expand Up @@ -686,8 +683,6 @@ def waitforconn(localip, localport, func, thread_pool=None, check_intv=0.1, err_
"""

# Check the args
if localip is not None and not _is_valid_ip_address(localip):
raise RepyArgumentError("Invalid Local IP address specified!")
if thread_pool is not None and "ThreadPool" not in str(type(thread_pool)):
raise RepyArgumentError("Invalid object provided for the thread pool!")
if type(check_intv) not in [int, float]:
Expand Down