9
9
10
10
namespace httpsserver {
11
11
12
- HTTPRequest::HTTPRequest (ConnectionContext * con, HTTPHeaders * headers, ResourceParameters * params):
12
+ HTTPRequest::HTTPRequest (ConnectionContext * con, HTTPHeaders * headers, ResourceParameters * params, std::string requestString, std::string method ):
13
13
_con (con),
14
14
_headers (headers),
15
- _params (params) {
15
+ _params (params),
16
+ _requestString (requestString),
17
+ _method (method) {
16
18
17
19
HTTPHeader * contentLength = headers->get (" Content-Length" );
18
20
if (contentLength == NULL ) {
@@ -43,6 +45,10 @@ std::string HTTPRequest::getHeader(std::string name) {
43
45
}
44
46
}
45
47
48
+ void HTTPRequest::setHeader (std::string name, std::string value) {
49
+ _headers->set (new HTTPHeader (name, value));
50
+ }
51
+
46
52
size_t HTTPRequest::readBytes (byte * buffer, size_t length) {
47
53
48
54
// Limit reading to content length
@@ -67,6 +73,14 @@ size_t HTTPRequest::getContentLength() {
67
73
return _remainingContent;
68
74
}
69
75
76
+ std::string HTTPRequest::getRequestString () {
77
+ return _requestString;
78
+ }
79
+
80
+ std::string HTTPRequest::getMethod () {
81
+ return _method;
82
+ }
83
+
70
84
bool HTTPRequest::requestComplete () {
71
85
if (_contentLengthSet) {
72
86
// If we have a content size, rely on it.
@@ -87,6 +101,59 @@ void HTTPRequest::discardRequestBody() {
87
101
}
88
102
}
89
103
104
+ std::string HTTPRequest::getBasicAuthUser () {
105
+ std::string token = decodeBasicAuthToken ();
106
+ size_t splitpoint = token.find (" :" );
107
+ if (splitpoint != std::string::npos && splitpoint > 0 ) {
108
+ return token.substr (0 , splitpoint);
109
+ } else {
110
+ return std::string ();
111
+ }
112
+ }
113
+
114
+ std::string HTTPRequest::getBasicAuthPassword () {
115
+ std::string token = decodeBasicAuthToken ();
116
+ size_t splitpoint = token.find (" :" );
117
+ if (splitpoint != std::string::npos && splitpoint > 0 ) {
118
+ return token.substr (splitpoint+1 );
119
+ } else {
120
+ return std::string ();
121
+ }
122
+ }
123
+
124
+ std::string HTTPRequest::decodeBasicAuthToken () {
125
+ std::string basicAuthString = getHeader (" Authorization" );
126
+ // Get the length of the token
127
+ size_t sourceLength = basicAuthString.length ();
128
+ // Only handle basic auth tokens
129
+ if (basicAuthString.substr (0 , 6 ) != " Basic " ) {
130
+ return std::string ();
131
+ }
132
+ // If the token is too long, skip
133
+ if (sourceLength > 100 ) {
134
+ return std::string ();
135
+ } else {
136
+ // Try to decode. As we are using mbedtls anyway, we can use that function
137
+ unsigned char * bufOut = new unsigned char [basicAuthString.length ()];
138
+ size_t outputLength = 0 ;
139
+ int res = mbedtls_base64_decode (
140
+ bufOut,
141
+ sourceLength,
142
+ &outputLength,
143
+ ((const unsigned char *)basicAuthString.substr (6 ).c_str ()), // Strip "Basic "
144
+ sourceLength - 6 // Strip "Basic "
145
+ );
146
+ // Failure of decoding
147
+ if (res != 0 ) {
148
+ delete[] bufOut;
149
+ return std::string ();
150
+ }
151
+ std::string tokenRes = std::string ((char *)bufOut, outputLength);
152
+ delete[] bufOut;
153
+ return tokenRes;
154
+ }
155
+ }
156
+
90
157
bool HTTPRequest::isSecure () {
91
158
return _con->isSecure ();
92
159
}
0 commit comments