1 /*
2  * Archttp - A highly performant web framework written in D.
3  *
4  * Copyright (C) 2021-2022 Kerisy.com
5  *
6  * Website: https://www.kerisy.com
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module archttp.HttpRequest;
13 
14 import archttp.Url;
15 
16 public import archttp.HttpMethod;
17 public import archttp.MultiPart;
18 
19 class HttpRequest
20 {
21     private
22     {
23         HttpMethod     _method;
24         Url            _uri;
25         string         _path;
26         string         _httpVersion;
27         string[string] _headers;
28         string         _body;
29     }
30 
31     public
32     {
33 
34         string[string] query;
35         string[string] parameters;
36         string[string] fields;
37         MultiPart[] files;
38     }
39 
40 public:
41 
42     ~ this()
43     {
44         // TODO clean upload files
45         // foreach ( file ; _files)
46         // {
47         //     // remove(file.tmpfile);
48         // }
49     }
50 
51     /*
52      * Set the HTTP method of this request.
53      *
54      * @param method the HTTP method
55      */
56     void method(HttpMethod method)
57     {
58         _method = method;
59     }
60 
61     /*
62      * Set the destination of this request.
63      *
64      * The destination is the URL path of the request, used to determine which resource is being
65      * requested.
66      *
67      * @param destination the URI of the request
68      */
69     void uri(Url uri)
70     {
71         _uri = uri;
72         _path = _uri.path;
73     }
74 
75     void path(string path)
76     {
77         _path = path;
78     }
79 
80     /*
81      * Set the HTTP version of this request.
82      *
83      * Sets the HTTP protocol version of this request.
84      *
85      * @param http_version the HTTP protocol version
86      */
87     void httpVersion(string http_version)
88     {
89         // TODO
90     }
91 
92     /*
93      * Set a header value of this request.
94      *
95      * @param header the key of the header to be set
96      * @param value the value of the header
97      */
98     void header(string header, string value)
99     {
100         _headers[header] = value;
101     }
102 
103     /*
104      * Set the body of the request.
105      *
106      * @param body the body of the request
107      */
108     void body(string body)
109     {
110         _body = body;
111     }
112 
113     /*
114      * Obtain a reference to the URL of this request.
115      *
116      * @return a reference to the URL
117      */
118     Url uri()
119     {
120         return _uri;
121     }
122 
123     string path()
124     {
125         return _path;
126     }
127 
128     /*
129      * Get the HTTP method of the request.
130      *
131      * @return HTTP method of the request
132      */
133     HttpMethod method()
134     {
135         return _method;
136     }
137 
138     /*
139      * Get the HTTP version of the request.
140      *
141      * @return HTTP version of the request
142      */
143     string httpVersion()
144     {
145         // TODO
146         
147         return null;
148     }
149 
150     /*
151      * Get a header value from this request.
152      *
153      * @param name the key of the header to obtain
154      *
155      * @return either the header value, or an empty string if the header does not exist
156      */
157     string header(string name)
158     {
159         return _headers.get(name, "");
160     }
161 
162     string[string] headers()
163     {
164         return _headers;
165     }
166 
167     /*
168      * Get the body of the request.
169      *
170      * @return the body of the request
171      */
172     string body()
173     {
174         return _body;
175     }
176 }