Skip to content

Commit

Permalink
http server, draft
Browse files Browse the repository at this point in the history
  • Loading branch information
kmalloc committed Dec 17, 2013
1 parent 9312124 commit 2995bdc
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ plan:

2) buffer managment.(done)

3) socket server based on epoll(done.06/12).
3) socket server based on epoll(done).

4) lock-free loging framework:queue/stack.(done)

5) http server (on going)


4 changes: 4 additions & 0 deletions net/SocketServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class SocketServer: public noncopyable
// to make sure that handler is fast as possible
void RegisterSocketEventHandler(SocketEventHandler handler);

public:

static const int max_conn_id;

private:

ServerImpl* impl_;
Expand Down
90 changes: 90 additions & 0 deletions net/http/HttpServer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "HttpServer.h"

#include "net/SocketServer.h"

#include "thread/ITask.h"
#include "thread/Thread.h"
#include "misc/LockFreeBuffer.h"

class HttpBuffer:public NonCopyable
{
public:

HttpBuffer();
~HttpBuffer();

void Consume(int sz);
void Append(const char* data);
char* Find(char c) const;
char* Find(const char* str) const;

private:

char* buff_; // allocate from lockfree buffer pool
int start_;
int end_;
int cur_;
};

class HttpContext: public NonCopyable
{
public:

HttpContext();
~HttpContext();

enum
{
HS_REQUEST_LINE,
HS_HEADER,
HS_BODY,
HS_INVALID
};

private:

bool keepalive_;
char* buff_;
int curStage_;
};

class HttpTask: public ITask
{
public:

HttpTask();
~HttpTask();

virtual void Run();

private:

HttpContext context_;
};

class HttpImpl:public ThreadBase
{
public:

HttpImpl(const char* addr);
~HttpImpl();

void StartServer();
void StopServer();

protected:

virtual void Run();

bool ParseRequestLine(HttpBuffer& buffer);
bool ParseHeader(HttpBuffer& buffer, map<string, string>& output);
string ParseBody(HttpBuffer& buffer);

private:

SocketServer tcpServer_;

// connection id to index into it
HttpTask conn_[];
};

24 changes: 24 additions & 0 deletions net/http/HttpServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __HTTP_SERVER_H__
#define __HTTP_SERVER_H__

#include "misc/NonCopyable.h"

class HttpImpl;

class HttpServer: public NonCopyable
{
public:

HttpServer(const char* addr);
~HttpServer();

void StartServer();
void StopServer();

private:

HttpImpl* impl_;
};

#endif

2 changes: 2 additions & 0 deletions net/src/SocketServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1333,3 +1333,5 @@ int SocketServer::WatchRawSocket(int fd, uintptr_t opaque)
return impl_->WatchRawSocket(fd, opaque);
}

const int SocketServer::max_conn_id = MAX_SOCKET;

0 comments on commit 2995bdc

Please sign in to comment.