1
+ """
2
+ Implements basic HTTP request parsing. This does not support SSL or anything like that
3
+ but can be used to serve a simple web server if needed. The underlying process to jsonrpc
4
+ It can verifiably stand on its own if needed.
5
+ """
6
+ import socket
7
+ from .netmgr import NetMgr
1
8
class HTTPRequest :
2
9
3
10
raw_msg = ""
@@ -11,6 +18,11 @@ class HTTPRequest:
11
18
protocol = ""
12
19
13
20
def __init__ (self ,nm ,sock ):
21
+ """
22
+ Initializes the http server, it requires the following parameters
23
+ nm: A connected NetMgr object
24
+ sock: A socket that is successfully listening
25
+ """
14
26
self .hsock = sock
15
27
self .nmgr = nm
16
28
self .verbose = nm .verbose
@@ -20,15 +32,27 @@ def __init__(self,nm,sock):
20
32
self ._parse ()
21
33
22
34
def socket (self ):
35
+ """
36
+ Returns the listening socket used to create this object
37
+ """
23
38
return self .hsock
24
39
25
40
def net_manager (self ):
41
+ """
42
+ Returns the connected NetMgr object used to construct the object
43
+ """
26
44
return self .nmgr
27
45
28
46
def raw_message (self ):
47
+ """
48
+ Returns the raw message read from the socket to process
49
+ """
29
50
return self .raw_msg
30
51
31
52
def _parse (self ):
53
+ """
54
+ Parses the message filling in the method, url, headers, and raw message sent.
55
+ """
32
56
if self .verbose :
33
57
print ("HTTPRequest::parse" )
34
58
try :
@@ -57,15 +81,33 @@ def _parse(self):
57
81
return True
58
82
59
83
def method (self ):
84
+ """
85
+ The method used for this request, usually POST or GET
86
+ """
60
87
return self .method
88
+
61
89
def url (self ):
90
+ """
91
+ The URL being requested
92
+ """
62
93
return self .uri
94
+
63
95
def protocol (self ):
96
+ """
97
+ The request protocol, usually HTTP/1.1
98
+ """
64
99
return self .protocol
100
+
65
101
def payload (self ):
102
+ """
103
+ The raw data sent after the headers. Will be blank if its a get request
104
+ """
66
105
return self .payload
67
106
68
107
def headerval (self ,key ):
108
+ """
109
+ Returns a case insensitive match for a header and retrieve its value
110
+ """
69
111
if self .verbose :
70
112
print (f"HTTPRequest::headerval ({ key } )" )
71
113
0 commit comments