-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_serv.c
161 lines (141 loc) · 3.52 KB
/
web_serv.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BUF_SIZE 1024
#define SMALL_BUF 100
void * request_handler(void * arg);
void send_data(FILE *fp, char *ct, char *file_name);
char * content_type(char *file);
void send_error(FILE *fp);
void error_handling(char *message);
int main(int argc, char const *argv[])
{
int serv_sock, clnt_sock;
struct sockaddr_in serv_adr, clnt_adr;
socklen_t clnt_adr_size;
// char buf[BUF_SIZE];
pthread_t t_id;
if(argc != 2)
{
printf("Usage :%s <port>\n", argv[0]);
exit(0);
}
serv_sock = socket(PF_INET, SOCK_STREAM,0);
memset(&serv_adr,0,sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock,(struct sockaddr*)&serv_adr,sizeof(serv_adr)) == -1)
error_handling("bind err");
if(listen(serv_sock, 20) == -1)
error_handling("listen err");
while(1)
{
clnt_adr_size = sizeof(clnt_adr);
clnt_sock = accept(serv_sock, (struct sockaddr *)&clnt_adr, &clnt_adr_size);
printf("Connect Request :%s:%d\n", inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port));
pthread_create(&t_id, NULL, request_handler, (void *)&clnt_sock);
pthread_detach(t_id);
}
close(serv_sock);
return 0;
}
void * request_handler(void *arg)
{
int clnt_sock = *((int*)arg);
char req_line[SMALL_BUF];
FILE *clnt_read;
FILE *clnt_write;
char method[10];
char ct[5];
char file_name[30];
clnt_read = fdopen(clnt_sock, "r");
clnt_write = fdopen(dup(clnt_sock), "w");
fgets(req_line, SMALL_BUF, clnt_read);
if(!strcmp(req_line, "HTTP://"))
{
send_error(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return NULL;
}
strcpy(method, strtok(req_line, " /"));
strcpy(file_name, strtok(NULL, " /"));
strcpy(ct,content_type(file_name));
if(strcmp(method,"GET") != 0)
{
send_error(clnt_write);
fclose(clnt_read);
fclose(clnt_write);
return NULL;
}
fclose(clnt_read);
send_data(clnt_write, ct, file_name);
return NULL;
}
void send_data(FILE *fp, char *ct, char *file_name)
{
char protocol[]="HTTP/1.0 200 OK\r\n";
char server[]="Server:Linux Web Server \r\n";
char cnt_len[]="Content-length:2048\r\n";
char cnt_type[SMALL_BUF];
char buf[BUF_SIZE];
FILE *send_file;
sprintf(cnt_type, "Content-type:%s\r\n\r\n",ct);
send_file = fopen(file_name,"r");
if(send_file == NULL)
{
send_error(fp);
return;
}
/*send head message*/
fputs(protocol, fp);
fputs(server, fp);
fputs(cnt_len, fp);
fputs(cnt_type, fp);
/*send body data*/
while(fgets(buf, BUF_SIZE, send_file) != NULL)
{
fputs(buf, fp);
fflush(fp);
}
fflush(fp);
fclose(fp);
}
char * content_type(char *file)
{
char extension[SMALL_BUF];
char file_name[SMALL_BUF];
strcpy(file_name, file);
strtok(file_name,".");
strcpy(extension, strtok(NULL,"."));
if(!strcmp(extension,"html") || !strcmp(extension, "htm"))
return "text/html";
else
return "text/plain";
}
void send_error(FILE *fp)
{
char protocol[]="HTTP/1.0 400 Bad Request\r\n";
char server[]="Server:Linux Web Server \r\n";
char cnt_len[]="Content-length:2048\r\n";
char cnt_type[]="Content-type:text/html\r\n\r\n";
/*char content[]="<html><head><title>NETWORK</title></head>"
"<body><font size=+5><br>err happened!"
"</font></body></html>";*/
fputs(protocol, fp);
fputs(server, fp);
fputs(cnt_len, fp);
fputs(cnt_type, fp);
fflush(fp);
}
void error_handling(char *message)
{
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}