MCPcopy
hub / github.com/USArmyResearchLab/Dshell / http_handler

Method http_handler

dshell/plugins/http/riphttp.py:64–108  ·  view source on GitHub ↗
(self, conn, request, response)

Source from the content-addressed store, hash-verified

62 sys.exit(1)
63
64 def http_handler(self, conn, request, response):
65 if (not self.direction or self.direction == 'cs') and request and request.method == "POST" and request.body:
66 if not self.content_filter or self.content_filter.search(request.headers.get('content-type', '')):
67 payload = request
68 elif (not self.direction or self.direction == 'sc') and response and response.status[0] == '2':
69 if not self.content_filter or self.content_filter.search(response.headers.get('content-type', '')):
70 payload = response
71 else:
72 payload = None
73
74 if not payload:
75 # Connection did not match any filters, so get rid of it
76 return
77
78 host = request.headers.get('host', conn.serverip)
79 url = host + request.uri
80
81 if url in self.openfiles:
82 # File is already open, so just insert the new data
83 s, e = self.openfiles[url].handleresponse(response)
84 self.logger.debug("{0!r} --> Range: {1} - {2}".format(url, s, e))
85 else:
86 # A new file!
87 filename = request.uri.split('?', 1)[0].split('/')[-1]
88 if self.name_filter and self.name_filter.search(filename):
89 # Filename did not match filter, so get rid of it
90 return
91 if not filename:
92 # Assume index.html if there is no filename
93 filename = "index.html"
94 if self.append_conn:
95 filename += "_{0}-{1}".format(conn.serverip, conn.clientip)
96 if self.append_ts:
97 filename += "_{}".format(conn.ts)
98 while os.path.exists(os.path.join(self.outdir, filename)):
99 filename += "_"
100 self.write("New file {} ({})".format(filename, url), **conn.info(), dir_arrow="<-")
101 self.openfiles[url] = HTTPFile(os.path.join(self.outdir, filename), self)
102 s, e = self.openfiles[url].handleresponse(payload)
103 self.logger.debug("{0!r} --> Range: {1} - {2}".format(url, s, e))
104 if self.openfiles[url].done():
105 self.write("File done {} ({})".format(filename, url), **conn.info(), dir_arrow="<-")
106 del self.openfiles[url]
107
108 return conn, request, response
109
110
111class HTTPFile(object):

Callers

nothing calls this directly

Calls 6

HTTPFileClass · 0.85
handleresponseMethod · 0.80
debugMethod · 0.80
doneMethod · 0.80
writeMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected