bool routeh(Request request, const char * path) { char * queryPath = getQueryPath(request.reqStr); if (strcmp(queryPath,path)==0) { free(queryPath); char ** headers = sendAndReceiveHeaders(request.client); if (headers) freeHeaders(headers); return true; } else { free(queryPath); return false; } }
bool routefh(Request request, const char * path, void (* function)(int,char *, char*)) { char * queryPath = getQueryPath(request.reqStr); if (strcmp(queryPath,path)==0) { free(queryPath); char ** headers = sendAndReceiveHeaders(request.client); function(request.client,request.reqStr,request.method); if (headers) freeHeaders(headers); return true; } else { free(queryPath); return false; } }
void free_fd_data(FdData * fd) { free(fd->readBuffer); free(fd->method); free(fd->uri); freeHeaders(fd->headers); free(fd->ver); fd->readBufferIdx = 0; fd->readBufferLen = 0; fd->methodIdx = 0; fd->uriIdx = 0; fd->verIdx = 0; fd->headersIdx = 0; fd->withinHeaderIdx = 0; }
boolean AtMegaWebServer::processRequest() { client_ = server_.available(); if (!client_.connected() || !client_.available()) { return false; } #if DEBUG Serial << F("WebServer: New request:\n"); #endif if(!readLine() || !*buffer){ sendHttpResult(408); // 408 Request Time-out client_.stop(); return false; } char* start = buffer; while(isspace(*start)) start++; if (!strncmp("GET", start, 3)) { request_type_ = GET; } else if (!strncmp("POST", start, 4)) { request_type_ = POST; } else if (!strncmp("PUT", start, 3)) { request_type_ = PUT; } else if (!strncmp("DELETE", start, 6)) { request_type_ = DELETE; } else if (!strncmp("MOVE", start, 4)) { request_type_ = MOVE; }else request_type_ = UNKNOWN_REQUEST; while(*start && !isspace(*start)) start++; // end of request_type_ while(*start && isspace(*start)) start++; // skip spaces, begin of path unescapeChars(start); // decode %-sequences of path char *end = start; while(*end && !isspace(*end)) end++; // end of path path_ = (char*) malloc_check(end - start + 1); if (path_) { memcpy(path_, start, end - start); path_[end - start] = 0; } boolean found = false, success = false; while(readLine()){ if(!*buffer){ success = true; // there are 2 x CRLF at end of header break; } found |= assignHeaderValue(); } // if found == true: at least 1 header value could be assigned // success indicates that there 2 x CRLF at end, so every thing is fine // if not you can sendHttpResult(404); or try to find a handler, maybe the 1st line // has been correct. If this fails sendHttpResult(404); // is called anyhow // Header processing finished. Identify the handler to call. boolean should_close = true; found = false; for (int i = 0; handlers_[i].path; i++) { int len = strlen(handlers_[i].path); boolean match = !strcmp(path_, handlers_[i].path); if (!match && handlers_[i].path[len - 1] == '*') { match = !strncmp(path_, handlers_[i].path, len - 1); } if (match && (handlers_[i].type == ANY || handlers_[i].type == request_type_)) { found = true; should_close = (handlers_[i].handler)(*this); break; } } if (!found) { sendHttpResult(404); } if (should_close) { client_.stop(); } freeHeaders(); free(path_); return true; }