@@ -126,6 +126,7 @@ bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
126126 std::string& http_method, std::string& http_uri)
127127{
128128 std::string str;
129+ // This strips the \n but does NOT strip any extra \r, such as the \r\n for Windows. See below.
129130 std::getline (stream, str);
130131
131132 // HTTP request line is space-delimited
@@ -144,15 +145,22 @@ bool ReadHTTPRequestLine(std::basic_istream<char>& stream, int &proto,
144145 if (http_uri.size () == 0 || http_uri[0 ] != ' /' )
145146 return false ;
146147
147- // parse proto, if present
148- std::string strProto;
149- if (vWords.size () > 2 )
150- strProto = vWords[2 ];
148+ // Parse proto, if present. If not present, return true to shorten processing.
149+ if (vWords.size () != 3 ) return true ;
151150
152- proto = 0 ;
153- const char *ver = strstr (strProto.c_str (), " HTTP/1." );
154- if (ver != nullptr && !ParseInt (std::string (ver+7 ), &proto)) {
155- throw std::invalid_argument (" Unable to parse proto." );
151+ std::string strProto = vWords[2 ];
152+
153+ strProto.pop_back ();
154+ size_t length = strProto.length ();
155+
156+ size_t start_pos = strProto.find (" HTTP/1." );
157+
158+ if (start_pos != std::string::npos && length - start_pos > 7 ) {
159+ strProto = strProto.substr (start_pos + 7 );
160+
161+ if (!ParseInt (strProto, &proto)) {
162+ return error (" %s: Unable to parse protocol in HTTP string: %s" , __func__, strProto);
163+ }
156164 }
157165
158166 return true ;
@@ -166,16 +174,23 @@ int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
166174 boost::split (vWords, str, boost::is_any_of (" " ));
167175 if (vWords.size () < 2 )
168176 return HTTP_INTERNAL_SERVER_ERROR;
169- proto = 0 ;
170- const char *ver = strstr (str.c_str (), " HTTP/1." );
171- if (ver != nullptr && !ParseInt (std::string (ver+7 ), &proto)) {
172- throw std::invalid_argument (" Unable to parse proto." );
177+ str.pop_back ();
178+
179+ size_t start_pos = str.find (" HTTP/1." );
180+
181+ if (start_pos != std::string::npos && str.length () - start_pos > 7 ) {
182+ str = str.substr (start_pos + 7 );
183+
184+ if (!ParseInt (str, &proto)) {
185+ error (" %s: Unable to parse protocol in HTTP string: %s" , __func__, str);
186+ }
173187 }
174188
175189 int status = 0 ;
176190 if (!ParseInt (vWords[1 ], &status)) {
177- throw std::invalid_argument ( " Unable to parse status. " );
191+ error ( " %s: Unable to parse status: %s " , __func__, vWords[ 1 ] );
178192 }
193+
179194 return status;
180195}
181196
0 commit comments