-
Notifications
You must be signed in to change notification settings - Fork 5
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
5 changed files
with
123 additions
and
1 deletion.
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 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 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,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_[]; | ||
}; | ||
|
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,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 | ||
|
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