From 5b6fa60e98a7c986a9ab74b6fa4f6c1513252800 Mon Sep 17 00:00:00 2001 From: roshow Date: Fri, 15 Mar 2013 02:25:58 -0400 Subject: [PATCH 1/3] ability to use dictionary notation to pass parameters into url --- http_requester.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) mode change 100755 => 100644 http_requester.py diff --git a/http_requester.py b/http_requester.py old mode 100755 new mode 100644 index da564b7..c1863b1 --- a/http_requester.py +++ b/http_requester.py @@ -4,6 +4,8 @@ import socket import types import threading +import ast +import urllib gPrevHttpRequest = "" @@ -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 @@ -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": "*/*"} @@ -219,6 +227,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 @@ -229,6 +253,7 @@ def extractExtraHeaders(self, headerLines): proxyPort = 0 extra_headers = {} + params = "" if len(headerLines) > 1: for i in range(1, numLines): lastLine = (i == numLines - 1) @@ -246,7 +271,7 @@ def extractExtraHeaders(self, headerLines): requestPOSTBody = requestPOSTBody + headerLines[i] + lineBreak - return (extra_headers, requestPOSTBody, proxyURL, proxyPort) + return (extra_headers, requestPOSTBody, proxyURL, proxyPort, params) def getProxyURLandPort(self, proxyAddress): proxyURL = "" From 2751d343c36401405f61732a4f24152e63e69e23 Mon Sep 17 00:00:00 2001 From: roshow Date: Fri, 15 Mar 2013 02:55:44 -0400 Subject: [PATCH 2/3] fixed so it's only grabbed once --- http_requester.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/http_requester.py b/http_requester.py index c1863b1..eb5c222 100644 --- a/http_requester.py +++ b/http_requester.py @@ -199,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() @@ -257,13 +261,17 @@ def extractExtraHeaders(self, headerLines): 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): From e8978782550c9f399e1ff8c454ea13e8638bd6e8 Mon Sep 17 00:00:00 2001 From: Rolando Garcia Date: Fri, 29 Mar 2013 14:23:27 -0300 Subject: [PATCH 3/3] Update README.md --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 456fafc..8e579b5 100644 --- a/README.md +++ b/README.md @@ -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.