-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,345 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.13) | ||
project(mymuduo) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
set(http_src | ||
HttpContext.cc | ||
HttpContext.h | ||
HttpRequest.h | ||
HttpResponse.cc | ||
HttpResponse.h | ||
HttpServer.cc | ||
HttpServer.h) | ||
|
||
add_library(http ${http_src}) | ||
target_link_libraries(http reactor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2010, Shuo Chen. All rights reserved. | ||
// http://code.google.com/p/muduo/ | ||
// | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the License file. | ||
|
||
// Author: Shuo Chen (chenshuo at chenshuo dot com) | ||
// | ||
|
||
#include "../reactor/Buffer.h" | ||
#include "HttpContext.h" | ||
#include <algorithm> | ||
|
||
using namespace muduo; | ||
using namespace muduo::net; | ||
|
||
bool HttpContext::processRequestLine(const char* begin, const char* end) | ||
{ | ||
bool succeed = false; | ||
const char* start = begin; | ||
const char* space = std::find(start, end, ' '); | ||
if (space != end && request_.setMethod(start, space)) | ||
{ | ||
start = space+1; | ||
space = std::find(start, end, ' '); | ||
if (space != end) | ||
{ | ||
const char* question = std::find(start, space, '?'); | ||
if (question != space) | ||
{ | ||
request_.setPath(start, question); | ||
request_.setQuery(question, space); | ||
} | ||
else | ||
{ | ||
request_.setPath(start, space); | ||
} | ||
start = space+1; | ||
succeed = end-start == 8 && std::equal(start, end-1, "HTTP/1."); | ||
if (succeed) | ||
{ | ||
if (*(end-1) == '1') | ||
{ | ||
request_.setVersion(HttpRequest::kHttp11); | ||
} | ||
else if (*(end-1) == '0') | ||
{ | ||
request_.setVersion(HttpRequest::kHttp10); | ||
} | ||
else | ||
{ | ||
succeed = false; | ||
} | ||
} | ||
} | ||
} | ||
return succeed; | ||
} | ||
|
||
// return false if any error | ||
bool HttpContext::parseRequest(muduo::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) | ||
{ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2010, Shuo Chen. All rights reserved. | ||
// http://code.google.com/p/muduo/ | ||
// | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the License file. | ||
|
||
// Author: Shuo Chen (chenshuo at chenshuo dot com) | ||
// | ||
// This is an internal header file, you should not include this. | ||
|
||
#ifndef MUDUO_NET_HTTP_HTTPCONTEXT_H | ||
#define MUDUO_NET_HTTP_HTTPCONTEXT_H | ||
|
||
#include "../base/copyable.h" | ||
#include "../reactor/Buffer.h" | ||
|
||
#include "HttpRequest.h" | ||
|
||
namespace muduo | ||
{ | ||
namespace net | ||
{ | ||
|
||
class Buffer; | ||
|
||
class HttpContext : public muduo::copyable | ||
{ | ||
public: | ||
enum HttpRequestParseState | ||
{ | ||
kExpectRequestLine, | ||
kExpectHeaders, | ||
kExpectBody, | ||
kGotAll, | ||
}; | ||
|
||
HttpContext() | ||
: state_(kExpectRequestLine) | ||
{ | ||
} | ||
|
||
// default copy-ctor, dtor and assignment are fine | ||
|
||
// return false if any error | ||
bool parseRequest(muduo::Buffer* buf, Timestamp receiveTime); | ||
|
||
bool gotAll() const | ||
{ return state_ == kGotAll; } | ||
|
||
void reset() | ||
{ | ||
state_ = kExpectRequestLine; | ||
HttpRequest dummy; | ||
request_.swap(dummy); | ||
} | ||
|
||
const HttpRequest& request() const | ||
{ return request_; } | ||
|
||
HttpRequest& request() | ||
{ return request_; } | ||
|
||
private: | ||
bool processRequestLine(const char* begin, const char* end); | ||
|
||
HttpRequestParseState state_; | ||
HttpRequest request_; | ||
}; | ||
|
||
} // namespace net | ||
} // namespace muduo | ||
|
||
#endif // MUDUO_NET_HTTP_HTTPCONTEXT_H |
Oops, something went wrong.