/*static*/bool HttpRequest::parseRequest(const char* requestLineandHeader, size_t len, HttpRequest* req) { //解析Http消息头的第一行,即请求行,Method Location HttpVer : GET /index.html HTTP/1.1 const char* start = requestLineandHeader; const char* firstCRLF = strstr(start, CRLF); if (!firstCRLF) return false; if (!processRequestLine(start, firstCRLF, req)) return false; return processRequestHeaders(firstCRLF + 2, requestLineandHeader + len, req); }
// return false if any error bool HttpContext::parseRequest(Buffer* buf, Timestamp receiveTime) { bool ok = true; bool hasMore = true; while(hasMore) { if(state_ == kExpectRequestLine) { const char* crlf = buf->findCRLF(); if(crlf) { ok = processRequestLine(buf->peek(), crlf); if(ok) { request_.setReceiveTime(receiveTime); buf->retrieveUntil(crlf + 2); state_ = kExpectHeaders; } else { hasMore = false; } } else { hasMore = false; } } else if(state_ == kExpectHeaders) { /* Host:116.211.115.50\r\n * User-Agent:ikuacc\r\n */ const char* crlf = buf->findCRLF(); if(crlf) { const char* colon = std::find(buf->peek(), crlf, ':'); if(colon != crlf) { request_.addHeader(buf->peek(), colon, crlf); } else { // empty line, end of header // FIXME: state_ = kGotAll; hasMore = false; } buf->retrieveUntil(crlf + 2); } else { hasMore = false; } } else if(state_ == kExpectBody) { // FIXME: } } return ok; }