forked from lizj3624/https-github.com-mogutt-TTServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpConn.h
More file actions
165 lines (147 loc) · 4.88 KB
/
Copy pathHttpConn.h
File metadata and controls
165 lines (147 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*================================================================
* Copyright (C) 2014 All rights reserved.
*
* 文件名称:HttpConn.h
* 创 建 者:Zhang Yuanhao
* 邮 箱:[email protected]
* 创建日期:2014年07月29日
* 描 述:
*
#pragma once
================================================================*/
#ifndef __HTTP_CONN_H__
#define __HTTP_CONN_H__
#include "util.h"
#if (MSFS_LINUX)
#include <sys/sendfile.h>
#elif (MSFS_BSD)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#endif
#include <pthread.h>
#include "netlib.h"
#include "SimpleBuffer.h"
#include "FileManager.h"
#include "ConfigFileReader.h"
#include "ThreadPool.h"
#include "HttpParserWrapper.h"
#define HTTP_CONN_TIMEOUT 30000
#define HTTP_UPLOAD_MAX 0xA00000 //10M
#define BOUNDARY_MARK "boundary="
#define HTTP_END_MARK "\r\n\r\n"
#define CONTENT_TYPE "Content-Type:"
#define CONTENT_DISPOSITION "Content-Disposition:"
#define READ_BUF_SIZE 0x100000 //1M
#define HTTP_RESPONSE_HEADER "HTTP/1.1 200 OK\r\n"\
"Connection:close\r\n"\
"Content-Length:%d\r\n"\
"Content-Type:multipart/form-data\r\n\r\n"
#define HTTP_RESPONSE_EXTEND "HTTP/1.1 200 OK\r\n"\
"Connection:close\r\n"\
"Content-Length:%d\r\n"\
"Content-Type:multipart/form-data\r\n\r\n"
#define HTTP_RESPONSE_HTML "HTTP/1.1 200 OK\r\n"\
"Connection:close\r\n"\
"Content-Length:%d\r\n"\
"Content-Type:text/html;charset=utf-8\r\n\r\n%s"
#define HTTP_RESPONSE_HTML_MAX 1024
#define HTTP_RESPONSE_403 "HTTP/1.1 403 Access Forbidden\r\n"\
"Content-Length: 0\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html;charset=utf-8\r\n\r\n"
#define HTTP_RESPONSE_403_LEN strlen(HTTP_RESPONSE_403)
#define HTTP_RESPONSE_404 "HTTP/1.1 404 Not Found\r\n"\
"Content-Length: 0\r\n"\
"Connection: close\r\n"\
"Content-Type: text/html;charset=utf-8\r\n\r\n"
#define HTTP_RESPONSE_404_LEN strlen(HTTP_RESPONSE_404)
#define HTTP_RESPONSE_500 "HTTP/1.1 500 Internal Server Error\r\n"\
"Connection:close\r\n"\
"Content-Length:0\r\n"\
"Content-Type:text/html;charset=utf-8\r\n\r\n"
#define HTTP_RESPONSE_500_LEN strlen(HTTP_RESPONSE_500)
using namespace msfs;
enum
{
CONN_STATE_IDLE, CONN_STATE_CONNECTED, CONN_STATE_OPEN, CONN_STATE_CLOSED,
};
extern FileManager * g_fileManager;
extern CConfigFileReader config_file;
extern CThreadPool g_PostThreadPool;
extern CThreadPool g_GetThreadPool;
typedef struct {
uint32_t conn_handle;
int method;
int nContentLen;
string strAccessHost;
char* pContent;
string strUrl;
string strContentType;
}Request_t;
typedef struct {
uint32_t conn_handle;
char* pContent;
uint32_t content_len;
} Response_t;
class CHttpConn;
class CHttpTask: public CTask
{
public:
CHttpTask(Request_t request);
virtual ~CHttpTask();
void run();
void OnUpload();
void OnDownload();
private:
uint32_t m_ConnHandle;
int m_nMethod;
string m_strUrl;
string m_strContentType;
char* m_pContent;
int m_nContentLen;
string m_strAccessHost;
};
class CHttpConn: public CRefObject
{
public:
CHttpConn();
virtual ~CHttpConn();
uint32_t GetConnHandle()
{
return m_conn_handle;
}
char* GetPeerIP()
{
return (char*) m_peer_ip.c_str();
}
int Send(void* data, int len);
void Close();
void OnConnect(net_handle_t handle);
void OnRead();
void OnWrite();
void OnClose();
void OnTimer(uint64_t curr_tick);
void OnSendComplete();
static void AddResponsePdu(uint32_t conn_handle, char* pContent, int nLen); // 工作线程调用
static void SendResponsePduList(); // 主线程调用
protected:
net_handle_t m_sock_handle;
uint32_t m_conn_handle;
bool m_busy;
uint32_t m_state;
string m_peer_ip;
uint16_t m_peer_port;
string m_access_host;
CSimpleBuffer m_in_buf;
CSimpleBuffer m_out_buf;
uint64_t m_last_send_tick;
uint64_t m_last_recv_tick;
CHttpParserWrapper m_HttpParser;
static CThreadLock s_list_lock;
static list<Response_t*> s_response_pdu_list; // 主线程发送回复消息
};
typedef hash_map<uint32_t, CHttpConn*> HttpConnMap_t;
CHttpConn* FindHttpConnByHandle(uint32_t handle);
void init_http_conn();
#endif