-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_service.h
123 lines (101 loc) · 2.48 KB
/
http_service.h
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
/*
http_service.h - Definition of HTTP Service
Author: Wenyu "Hearson" Zhang
E-mail: zhangw@purdue.edu
This file is a part of:
Project 3 - Building a HTTP Server
CS290 - System Programming, Fall 2010
Purdue University
For more information, please contact the author.
*/
#ifndef _HTTP_SERVICE_H_
#define _HTTP_SERVICE_H_
#include <string>
#include "service.h"
typedef enum {
ERROR = -1,
DNE = 0,
DIRECTORY = 1,
BINARY,
TEXT,
HTML,
JPEG,
GIF,
PNG,
MP4
} DocumentType;
typedef struct {
std::string str;
DocumentType type;
} MimeCombination;
const MimeCombination mapSuffixType[] = {
{ "GIF", GIF },
{ "HTM", HTML },
{ "HTML", HTML },
{ "JPEG", JPEG },
{ "JPG", JPEG },
{ "PNG", PNG },
{ "H", TEXT },
{ "CPP", TEXT },
{ "CC", TEXT },
{ "C", TEXT },
{ "TXT", TEXT },
{ "MP4", MP4 }
};
const MimeCombination mapTypeMime[] = {
{ "binary", BINARY },
{ "image/gif", GIF },
{ "image/jpeg", JPEG },
{ "image/png", PNG },
{ "text/html", HTML },
{ "text/plain", TEXT },
{ "video/mp4", MP4 }
};
const MimeCombination mapTypeIcon[] = {
{ "unknown.gif", BINARY },
{ "image.gif", GIF },
{ "image.gif", JPEG },
{ "image.gif", PNG },
{ "index.gif", HTML },
{ "text.gif", TEXT },
{ "image.gif", MP4 }
};
//////////////////////////////////////////////////////////////////////////////////
const int mapSuffixTypeCount = sizeof(mapSuffixType) / sizeof(mapSuffixType[0]);
const int mapTypeMimeCount = sizeof(mapTypeMime) / sizeof(mapTypeMime[0]);
const int mapTypeIconCount = sizeof(mapTypeIcon) / sizeof(mapTypeIcon[0]);
class HTTPService : public Service {
public:
HTTPService();
virtual ~HTTPService();
virtual Service* clone();
virtual int serviceHandler();
virtual int timeOutHandler();
virtual int initialize();
private:
int processRequest();
void cgiRun();
void showStatistics();
void showLog();
void sendDirectory();
void sendError301(std::string newLocation);
void sendError404();
void sendError501();
void sendReply200(DocumentType docType);
private:
static const int bufferSize = 16 * 1024 * 1024;
char buffer[bufferSize];
std::string rootDir;
std::string requestedDocument;
std::string httpGetVars;
std::string protocolVersion;
std::string requestMethod;
std::string postContentLength;
bool healthy;
bool keepAlive;
long long startPosition, endPosition;
int fdDoc; // The range of file is [startPosition, endPosition)
int fdMem;
time_t timeStart;
};
#endif // _HTTP_SERVICE_H_