| 74 | } |
| 75 | |
| 76 | bool WebServer::_parseRequest(NetworkClient &client) { |
| 77 | // Read the first line of HTTP request |
| 78 | String req = client.readStringUntil('\r'); |
| 79 | client.readStringUntil('\n'); |
| 80 | //reset header value |
| 81 | if (_collectAllHeaders) { |
| 82 | // clear previous headers |
| 83 | collectAllHeaders(); |
| 84 | } else { |
| 85 | // clear previous headers |
| 86 | for (RequestArgument *header = _currentHeaders; header; header = header->next) { |
| 87 | header->value = String(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // First line of HTTP request looks like "GET /path HTTP/1.1" |
| 92 | // Retrieve the "/path" part by finding the spaces |
| 93 | int addr_start = req.indexOf(' '); |
| 94 | int addr_end = req.indexOf(' ', addr_start + 1); |
| 95 | if (addr_start == -1 || addr_end == -1) { |
| 96 | log_e("Invalid request: %s", req.c_str()); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | String methodStr = req.substring(0, addr_start); |
| 101 | String url = req.substring(addr_start + 1, addr_end); |
| 102 | String versionEnd = req.substring(addr_end + 8); |
| 103 | _currentVersion = atoi(versionEnd.c_str()); |
| 104 | String searchStr = ""; |
| 105 | int hasSearch = url.indexOf('?'); |
| 106 | if (hasSearch != -1) { |
| 107 | searchStr = url.substring(hasSearch + 1); |
| 108 | url = url.substring(0, hasSearch); |
| 109 | } |
| 110 | _currentUri = url; |
| 111 | _chunked = false; |
| 112 | _clientContentLength = 0; // not known yet, or invalid |
| 113 | |
| 114 | HTTPMethod method = HTTP_ANY; |
| 115 | size_t num_methods = sizeof(_http_method_str) / sizeof(const char *); |
| 116 | for (size_t i = 0; i < num_methods; i++) { |
| 117 | if (methodStr == _http_method_str[i]) { |
| 118 | method = (HTTPMethod)i; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | if (method == HTTP_ANY) { |
| 123 | log_e("Unknown HTTP Method: %s", methodStr.c_str()); |
| 124 | return false; |
| 125 | } |
| 126 | _currentMethod = method; |
| 127 | |
| 128 | log_v("method: %s url: %s search: %s", methodStr.c_str(), url.c_str(), searchStr.c_str()); |
| 129 | |
| 130 | //attach handler |
| 131 | RequestHandler *handler; |
| 132 | for (handler = _firstHandler; handler; handler = handler->next()) { |
| 133 | if (handler->canHandle(*this, _currentMethod, _currentUri)) { |
nothing calls this directly
no test coverage detected