Skip to content

Commit

Permalink
http
Browse files Browse the repository at this point in the history
  • Loading branch information
834810071 committed Mar 12, 2020
1 parent be035ed commit 12b3a21
Show file tree
Hide file tree
Showing 39 changed files with 1,345 additions and 81 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ADD_SUBDIRECTORY(book_study/Chapter06)
ADD_SUBDIRECTORY(book_study/Chapter07)
ADD_SUBDIRECTORY(book_study/Chapter08)
ADD_SUBDIRECTORY(book_study/问题记载)
ADD_SUBDIRECTORY(WebServer)
ADD_SUBDIRECTORY(http)
ADD_SUBDIRECTORY(http/test)

add_executable(mymuduo main.cpp)
8 changes: 0 additions & 8 deletions WebServer/CMakeLists.txt

This file was deleted.

16 changes: 16 additions & 0 deletions http/CMakeLists.txt
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)
119 changes: 119 additions & 0 deletions http/HttpContext.cc
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;
}
73 changes: 73 additions & 0 deletions http/HttpContext.h
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
Loading

0 comments on commit 12b3a21

Please sign in to comment.