99
1010namespace httpsserver {
1111
12- HTTPRequest::HTTPRequest (ConnectionContext * con, HTTPHeaders * headers, ResourceParameters * params):
12+ HTTPRequest::HTTPRequest (ConnectionContext * con, HTTPHeaders * headers, ResourceParameters * params, std::string requestString, std::string method ):
1313 _con (con),
1414 _headers (headers),
15- _params (params) {
15+ _params (params),
16+ _requestString (requestString),
17+ _method (method) {
1618
1719 HTTPHeader * contentLength = headers->get (" Content-Length" );
1820 if (contentLength == NULL ) {
@@ -43,6 +45,10 @@ std::string HTTPRequest::getHeader(std::string name) {
4345 }
4446}
4547
48+ void HTTPRequest::setHeader (std::string name, std::string value) {
49+ _headers->set (new HTTPHeader (name, value));
50+ }
51+
4652size_t HTTPRequest::readBytes (byte * buffer, size_t length) {
4753
4854 // Limit reading to content length
@@ -67,6 +73,14 @@ size_t HTTPRequest::getContentLength() {
6773 return _remainingContent;
6874}
6975
76+ std::string HTTPRequest::getRequestString () {
77+ return _requestString;
78+ }
79+
80+ std::string HTTPRequest::getMethod () {
81+ return _method;
82+ }
83+
7084bool HTTPRequest::requestComplete () {
7185 if (_contentLengthSet) {
7286 // If we have a content size, rely on it.
@@ -87,6 +101,59 @@ void HTTPRequest::discardRequestBody() {
87101 }
88102}
89103
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+
90157bool HTTPRequest::isSecure () {
91158 return _con->isSecure ();
92159}
0 commit comments