-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server_linux.cpp
More file actions
291 lines (262 loc) · 7.32 KB
/
http_server_linux.cpp
File metadata and controls
291 lines (262 loc) · 7.32 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SERVER_PORT 7778
#define MAX_IO_SIZE 15000
#define QLEN 5
#include <iostream>
using namespace std;
int readline(int fd,char *ptr,int maxlen)
{
int n, rc;
char c;
*ptr = 0;
//fprintf(stderr, "do readline, %d\n", fd);
for(n=1; n<maxlen; n++)
{
rc=read(fd,&c,1);
if(rc== 1)
{
*ptr++ = c;
if(c=='\n') break;
}
else if(rc==0)
{
if(n==1) return 0;
else break;
}
else return(-1);
}
return n;
}
int parseHttpMessage(string &str)
{
string strGet = "GET /";
string strHttp = "HTTP/";
int startPos = str.find(strGet);
int endPos = str.find(strHttp);
if(startPos == string::npos || endPos == string::npos){
fprintf(stderr, "GET or HTTP is lost!\n");
return -1;
}
str = str.substr(startPos + strGet.length(), endPos - strGet.length() - 1);
return 0;
}
int deleteChangeLine(char* buf, int iSize)
{
int size = iSize - 1;
buf[iSize] = '\0';
while(buf[size] == '\n' || buf[size] == '\r'){
buf[size--] = '\0';
}
return size;
}
void httpService(int sockfd)
{
char sInputbuf[1000];
string sInputString;
string sApplicationName;
char* vCommandWord[MAX_IO_SIZE];
//get the string of file name and parameters
setenv("PATH", ".", 1);
int iInputSize = readline(sockfd, sInputbuf, 1000);
iInputSize = deleteChangeLine(sInputbuf, iInputSize);
fprintf(stderr, "%s\n", sInputbuf);
sInputString.assign(sInputbuf);
if(parseHttpMessage(sInputString) == -1){
exit(EXIT_FAILURE);
}
//fprintf(stderr, "%s %d\n", sInputString.c_str(), sInputString.length());
sApplicationName = sInputString;
vCommandWord[0] = (char*) malloc(50);
bzero(vCommandWord[0], 50);
strcpy(vCommandWord[0], sApplicationName.c_str());
//set file name and parameters
int nParameterCount = 1;
int pos = sInputString.find("?");
if(pos != string::npos){
sApplicationName = sInputString.substr(0, pos);
bzero(vCommandWord[0], 50);
strcpy(vCommandWord[0], sApplicationName.c_str());
sInputString = sInputString.substr(pos+1);
for(int i=1; i<=15; i++){
int startpos = sInputString.find("=") + 1;
int endpos = sInputString.find("&");
if(startpos == string::npos)
break;
if(endpos == string::npos){//last parameter
if(startpos == sInputString.length())//empty parameter -> break
break;
}
else if(endpos == startpos)//empty parameter -> break
break;
vCommandWord[i] = (char*) malloc(32);
bzero(vCommandWord[i], 32);
strcpy(vCommandWord[i], sInputString.substr(startpos, endpos-startpos).c_str());
//printf("%d %d %s\n", startpos, endpos, vCommandWord[i]);
sInputString = sInputString.substr(endpos+1);
nParameterCount++;
if(endpos == string::npos)//last parameter
break;
}
}
vCommandWord[nParameterCount] = (char *) 0;
/*
fprintf(stderr, "%s ", sApplicationName.c_str());
for(int i=0; i<nParameterCount; i++){
fprintf(stderr, "%s ", vCommandWord[i]);
}
fprintf(stderr, "\n");
*/
if(dup2(sockfd, STDIN_FILENO) == -1){
write(STDERR_FILENO, "dup socket to output error!\n", 28);
exit(EXIT_FAILURE);
}
if(dup2(sockfd, STDOUT_FILENO) == -1){
write(STDERR_FILENO, "dup socket to output error!\n", 28);
exit(EXIT_FAILURE);
}
//printf("HTTP/1.1 200 OK\n");
//printf("Content-Type: text/html\n\n");
if(execvp(sApplicationName.c_str(), vCommandWord) == -1){
fprintf(stderr, "execvp: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
int passivesock(char* service, char* protocol, int qlen)
{
struct servent *pse; /* pointer to service information entry*/
struct protoent *ppe; /* pointer to protocol information entry*/
struct sockaddr_in sin; /* an Internet endpoint address */
int socketfd, type; /* socket descriptor and socket type */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
/* Map service name to port number */
/*
if ( pse = getservbyname(service, protocol) )
sin.sin_port = pse->s_port;
else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 ){
fprintf(stderr, "can't get \"%s\" service entry\n", service);
exit(EXIT_FAILURE);
}
*/
//use fix port: SERVER_PORT
if ( (sin.sin_port = htons(SERVER_PORT)) == 0 ){
fprintf(stderr, "can't get \"%s\" service entry\n", service);
exit(EXIT_FAILURE);
}
/* Map protocol name to protocol number */
if( (ppe = getprotobyname(protocol)) == 0){
fprintf(stderr, "can't get \"%s\" protocol entry\n", protocol);
exit(EXIT_FAILURE);
}
/* Use protocol to choose a socket type */
if (strcmp(protocol, "udp") == 0)
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
/* Allocate a socket */
socketfd = socket(PF_INET, type, ppe->p_proto);
if (socketfd < 0){
fprintf(stderr, "can't create socket: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
}
/* Bind the socket */
if (bind(socketfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
fprintf(stderr, "can't bind to %s port: %s\n", service, sys_errlist[errno]);
exit(EXIT_FAILURE);
}
if (type == SOCK_STREAM && listen(socketfd, qlen) < 0){
fprintf(stderr, "can't listen on %s port: %s\n", service, sys_errlist[errno]);
exit(EXIT_FAILURE);
}
return socketfd;
}
int passiveTCP(char* service, int qlen)
{
return passivesock(service, (char* )"tcp", qlen);
}
int TCPechod(int fd){
char buf[BUFSIZ];
int cc;
//fprintf(stderr, "do TCPechod\n, %d", BUFSIZ);
while (cc = readline(fd, buf, sizeof(buf))) {
//fprintf(stderr, "read string: %s\n", buf);
if (cc < 0){
fprintf(stderr, "echo read: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
}
if (write(fd, buf, cc) < 0){
fprintf(stderr, "echo write: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
}
}
return 0;
}
int main(int argc, char** argv){
//char *service = "echo"; /* service name or port number */
struct sockaddr_in fsin; /* the address of a client */
socklen_t alen; /* length of client's address */
int msock; /* master server socket */
int ssock; /* slave server socket */
char sClientIp[INET_ADDRSTRLEN];
int iClientPort;
int iShmFd; /* share memory fd */
int iUserId; /* User id*/
int iRootPid = getpid();
switch(argc){
case 1:
break;
case 2:
//service = argv[1];
break;
default:
fprintf(stderr, "usage: TCPechod [port]\n");
exit(EXIT_SUCCESS);
}
//msock = passiveTCP(service, QLEN);
msock = passiveTCP("echo", QLEN);
//fprintf(stderr, "The socket is ready.\n");
printf("Server start!\n");
while(true){
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *) &fsin, &alen);
//fprintf(stderr, "I got a client.\n");
if (ssock < 0) {
if (errno == EINTR)
continue;
fprintf(stderr, "accept: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
}
//Concurrent, Connection-Oriented Servers
int pid = fork();
switch (pid) {
case -1:
fprintf(stderr, "fork: %s\n", sys_errlist[errno]);
exit(EXIT_FAILURE);
case 0: /* child */
close(msock);
//do service
httpService(ssock);
//maybe I will come to here
close(ssock);
default: /* parent */
close(ssock);
//fprintf(stderr, "I am parent.\n");
break;
}
}
return 0;
}