Skip to content
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
Contact: [braindamageinc@gmail.com](mailto:braindamageinc@gmail.com)

##Summary
Makes HTTP requests using the selected text as URL + headers. Usefull for testing REST APIs from Sublime Text 2 editor.
Makes HTTP requests using the selected text as URL + headers. Usefull for testing REST APIs from Sublime Text 2 editor.

#Update (by roshow): Parameters for GET requests in dictionary
I made an update to http_requester.py so you can put in the parameters for a request on a separate line from the url. So, for example, instead of:

GET http://www.youtube.com/results?search_query=batman&filters=hd

You can write it like this:

GET http://www.youtube.com/results
{ 'search_query': 'batman', 'filters': 'hd' }

Note that for this to work, you have to write the dictionary in one line without breaks. The dictionary can be written in any line following the url -- I have the script looking for anything that begins with curly brackets.

##Update: Requests are threaded now, with progress report in status bar.

Expand Down
53 changes: 43 additions & 10 deletions http_requester.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import socket
import types
import threading
import ast
import urllib

gPrevHttpRequest = ""

Expand Down Expand Up @@ -37,7 +39,7 @@ class HttpRequester(threading.Thread):

HTTP_PROXY_HEADER = "USE_PROXY"

CONTENT_LENGTH_HEADER = "Content-lenght"
CONTENT_LENGTH_HEADER = "Content-length"

MAX_BYTES_BUFFER_SIZE = 8192

Expand Down Expand Up @@ -81,7 +83,13 @@ def run(self):
print requestType, " ", httpProtocol, " HOST ", url, " PORT ", port, " PAGE: ", request_page

# get request headers from the lines below the http address
(extra_headers, requestPOSTBody, proxyURL, proxyPort) = self.extractExtraHeaders(lines)
(extra_headers, requestPOSTBody, proxyURL, proxyPort, params) = self.extractExtraHeaders(lines)

#roshow Start: add any params to request_page by invoking extractParams method
params = self.extractParams(lines)
if params != False:
request_page = request_page + params
#roshow End

headers = {"User-Agent": FAKE_CURL_UA, "Accept": "*/*"}

Expand Down Expand Up @@ -191,12 +199,16 @@ def extractRequestParams(self, requestLine):
def getHeaderNameAndValueFromLine(self, line):
readingPOSTBody = False

#print "roshow"

line = line.lstrip()
line = line.rstrip()

if line == self.HTTP_POST_BODY_START:
readingPOSTBody = True
else:
#roshow Start: updated else to elif statement filtering out params:
elif line[0] != "{":
#roshow End
header_parts = line.split(":")
if len(header_parts) == 2:
header_name = header_parts[0].rstrip()
Expand All @@ -219,6 +231,22 @@ def getHeaderNameAndValueFromLine(self, line):

return (None, None, readingPOSTBody)

#roshow Start: extract any parameters object
def extractParams(self, headerLines):
params = False
numLines = len(headerLines)
for i in range(0, numLines):
if headerLines[i][0] == "{":
paramObj = ast.literal_eval(headerLines[i])
params = "?"
for key in paramObj:
params = params + key + "=" + urllib.quote(paramObj[key]) + "&"
params = params[:-1]

return params
#roshow End


def extractExtraHeaders(self, headerLines):
requestPOSTBody = ""
readingPOSTBody = False
Expand All @@ -229,24 +257,29 @@ def extractExtraHeaders(self, headerLines):
proxyPort = 0

extra_headers = {}
params = ""
if len(headerLines) > 1:
for i in range(1, numLines):
lastLine = (i == numLines - 1)
#roshow Start: Added "headerLines[i][0] != "{""
#if not(readingPOSTBody) and headerLines[i][0] != "{":
#roshow End
if not(readingPOSTBody):
(header_name, header_value, readingPOSTBody) = self.getHeaderNameAndValueFromLine(headerLines[i])
if header_name != None:
if header_name != self.HTTP_PROXY_HEADER:
extra_headers[header_name] = header_value
else:
(proxyURL, proxyPort) = self.getProxyURLandPort(header_value)
if headerLines[i][0] != "{":
(header_name, header_value, readingPOSTBody) = self.getHeaderNameAndValueFromLine(headerLines[i])
if header_name != None:
if header_name != self.HTTP_PROXY_HEADER:
extra_headers[header_name] = header_value
else:
(proxyURL, proxyPort) = self.getProxyURLandPort(header_value)
else: # read all following lines as HTTP POST body
lineBreak = ""
if not(lastLine):
lineBreak = "\n"

requestPOSTBody = requestPOSTBody + headerLines[i] + lineBreak

return (extra_headers, requestPOSTBody, proxyURL, proxyPort)
return (extra_headers, requestPOSTBody, proxyURL, proxyPort, params)

def getProxyURLandPort(self, proxyAddress):
proxyURL = ""
Expand Down