-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCHttpRequest.cpp
109 lines (96 loc) · 2.84 KB
/
CHttpRequest.cpp
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
//
// CHttpRequest.cpp
// TSHttpServer
//
// Created by zhaoxy on 14-4-23.
// Copyright (c) 2014年 tsinghua. All rights reserved.
//
#include "CHttpRequest.h"
#include "define.h"
#include <sys/socket.h>
using namespace std;
CHttpRequest::CHttpRequest(int tConnFd) {
connFd = tConnFd;
connection = "Close";
modifiedTime = "";
fileStart = 0;
fileEnd = 0;
fieldMap[TS_HTTP_HEADER_CONNECTION] = &CHttpRequest::handleConnection;
fieldMap[TS_HTTP_HEADER_AUTHORIZATION] = &CHttpRequest::handleAuthorization;
fieldMap[TS_HTTP_HEADER_RANGE] = &CHttpRequest::handleRange;
fieldMap[TS_HTTP_HEADER_IF_MOD_SINCE] = &CHttpRequest::handleIfModSince;
fieldMap[TS_HTTP_HEADER_CONTENT_LENGTH] = &CHttpRequest::handleContentLength;
}
void CHttpRequest::handleRequest(char *header) {
stringstream stream;
stream<<header;
int count = 0;
while (1) {
if (stream.eof()) {
break;
}
char line[1024];
stream.getline(line, sizeof(line));
if (strcmp(line, "")==0) {
continue;
}
stringstream lineStream;
lineStream<<line;
//first line
if (count == 0) {
lineStream>>method;
lineStream>>path;
lineStream>>version;
}else {
string fieldName;
lineStream>>fieldName;
//remove \r
line[strlen(line)-1] = '\0';
void(CHttpRequest::*func)(char*) = fieldMap[fieldName];
if (func!=NULL) {
(this->*func)(line+fieldName.length()+1);
}
}
count++;
}
//if method is post, continue receiving data
if (method.compare(TS_HTTP_METHOD_POST_S)==0) {
long recvLen = 0;
while (recvLen<contentLength) {
//读取缓存
char buff[4096];
//读取http header
int len = (int)recv(connFd, buff, sizeof(buff), 0);
buff[len] = '\0';
std::cout<<buff;
if (len<0) {
break;
}
recvLen+=len;
}
}
}
void CHttpRequest::handleConnection(char *field) {
if (ENABLE_KEEP_ALIVE) {
connection = string(field);
}
}
void CHttpRequest::handleAuthorization(char *field) {
char authName[10], authInfo[256];
sscanf(field, "%s %s", authName, authInfo);
authorize = string(authInfo);
}
void CHttpRequest::handleRange(char *field) {
if (strstr(field, "bytes=")==field) {
char *start = strtok(field+strlen("bytes="), "-");
fileStart = start==NULL?0:atol(start);
char *end = strtok(NULL, "-");
fileEnd = end==NULL?0:atol(end);
}
}
void CHttpRequest::handleIfModSince(char *field) {
modifiedTime = string(field);
}
void CHttpRequest::handleContentLength(char *field) {
contentLength = atol(field);
}