1
1
from memory import Span
2
- from lightbug_http.io.bytes import Bytes, bytes , ByteReader, ByteWriter
2
+ from lightbug_http.io.bytes import Bytes, bytes , ByteReader, ByteWriter, ByteView
3
3
from lightbug_http.header import Headers, HeaderKey, Header, write_header
4
4
from lightbug_http.cookie import RequestCookieJar
5
5
from lightbug_http.uri import URI
@@ -30,29 +30,30 @@ struct RequestMethod:
30
30
alias options = RequestMethod(" OPTIONS" )
31
31
32
32
33
- @value
34
- struct HTTPRequest (Writable , Stringable ):
35
- var headers : Headers
33
+ struct HTTPRequest[origin: Origin](Writable, Stringable):
34
+ var headers : Headers[origin]
36
35
var cookies : RequestCookieJar
37
36
var uri : URI
38
37
var body_raw : Bytes
39
38
40
- var method : String
41
- var protocol : String
39
+ var method : ByteView[origin]
40
+ var protocol : ByteView[origin]
42
41
43
42
var server_is_tls : Bool
44
43
var timeout : Duration
45
44
46
45
@ staticmethod
47
- fn from_bytes (addr : String, max_body_size : Int, b : Span[Byte]) raises -> HTTPRequest:
46
+ fn from_bytes (addr : String, max_body_size : Int, b : Span[Byte]) raises -> HTTPRequest[origin] :
48
47
var reader = ByteReader(b)
49
- var headers = Headers()
50
- var method : String
51
- var protocol : String
52
- var uri : String
48
+ var headers = Headers[origin] ()
49
+ var method : ByteView[origin]
50
+ var protocol : ByteView[origin]
51
+ var uri : ByteView[origin]
53
52
try :
54
53
var rest = headers.parse_raw(reader)
55
- method, uri, protocol = rest[0 ], rest[1 ], rest[2 ]
54
+ var method = rest[0 ]
55
+ var uri = rest[1 ]
56
+ var protocol = rest[2 ]
56
57
except e:
57
58
raise Error(" HTTPRequest.from_bytes: Failed to parse request headers: " + String(e))
58
59
@@ -67,7 +68,7 @@ struct HTTPRequest(Writable, Stringable):
67
68
raise Error(" HTTPRequest.from_bytes: Request body too large." )
68
69
69
70
var request = HTTPRequest(
70
- URI .parse(addr + uri), headers = headers, method = method, protocol = protocol, cookies = cookies
71
+ URI .parse(addr + String( uri)) , headers = headers, method = String( method) , protocol = String( protocol) , cookies = cookies
71
72
)
72
73
73
74
if content_length > 0 :
@@ -82,7 +83,7 @@ struct HTTPRequest(Writable, Stringable):
82
83
fn __init__ (
83
84
out self ,
84
85
uri : URI ,
85
- headers : Headers = Headers(),
86
+ headers : Headers[origin] = Headers[origin] (),
86
87
cookies : RequestCookieJar = RequestCookieJar(),
87
88
method : String = " GET" ,
88
89
protocol : String = strHttp11,
@@ -92,7 +93,7 @@ struct HTTPRequest(Writable, Stringable):
92
93
):
93
94
self .headers = headers
94
95
self .cookies = cookies
95
- self .method = method
96
+ self .method = ByteView( method.as_bytes())
96
97
self .protocol = protocol
97
98
self .uri = uri
98
99
self .body_raw = body
@@ -108,6 +109,14 @@ struct HTTPRequest(Writable, Stringable):
108
109
else :
109
110
self .headers[HeaderKey.HOST ] = uri.host
110
111
112
+ fn __copyinit__ (out self , existing : HTTPRequest[origin]):
113
+ self .headers = existing.headers
114
+ self .cookies = existing.cookies
115
+ self .uri = existing.uri
116
+ self .body_raw = existing.body_raw
117
+ self .method = existing.method
118
+ self .protocol = existing.protocol
119
+
111
120
fn get_body (self ) -> StringSlice[__origin_of(self .body_raw)]:
112
121
return StringSlice(unsafe_from_utf8 = Span(self .body_raw))
113
122
0 commit comments