forked from devimalka/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
192 lines (162 loc) · 5.83 KB
/
main.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
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
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT "8080"
#define BUFFSIZE 2048
#define RESSIZE 4096
#include "filehandle.h"
#include "httpresponse.h"
#include "routes.h"
int main(int argc, char *argv[]) {
int status;
struct addrinfo hints, *res;
int sockfd;
int bindstatus;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
status = getaddrinfo(NULL, PORT, &hints, &res);
if (status == -1) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
}
sockfd = socket(res->ai_family, res->ai_socktype, 0);
if (sockfd == -1) {
fprintf(stderr, "socket error %s\n", strerror(sockfd));
}
int opt = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
bindstatus = bind(sockfd, res->ai_addr, res->ai_addrlen);
if (bindstatus == -1) {
fprintf(stderr, "bind error %s\n", strerror(bindstatus));
}
freeaddrinfo(res);
int l;
l = listen(sockfd, 10);
if (l != 0) {
fprintf(stderr, "LISTEN ERROR %s\n", strerror(l));
}
while (1) {
struct sockaddr_in client_addr;
int newfd;
socklen_t theiraddr = sizeof client_addr;
newfd = accept(sockfd, (struct sockaddr *)&client_addr, &theiraddr);
if (newfd == -1) {
fprintf(stderr, "accept error %s\n", strerror(errno));
} else {
char str[INET_ADDRSTRLEN];
inet_ntop(client_addr.sin_family, (struct inaddr *)&client_addr.sin_addr,
str, INET_ADDRSTRLEN);
printf("Connection from %s\n", str);
}
char *request = requestpath(newfd);
char *filepath = get_file_path(request);
free(request);
char *ext = get_file_ext(filepath, '.');
printf("file ext is %s\n", ext);
char *path = removenslash(filepath);
printf("path is %s\n", path);
char *response = NULL;
if (strcmp(ext, ".html") == 0) {
if (strcmp(path, "") == 0 || strcmp(filepath, "index.html") == 0) {
response = content("index.html");
char *http_response = (char *)malloc(RESSIZE);
char *date = get_date_for_server();
int content_length = strlen(response);
int len, bytes_sent;
int n =
snprintf(http_response, RESSIZE,
"HTTP/1.1 200 OK\r\nAcess-Control-Allow-Origin: *"
"*\r\nConnection: Keep-Alive\r\nDate: %s\r\nContent-Type: "
"%s\r\nContent-Length: %d\r\n\n%s",
date, "text/html", content_length, response);
len = strlen(http_response);
bytes_sent = send(newfd, http_response, len, 0);
shutdown(newfd, SHUT_WR);
free(date);
free(http_response);
} else {
response = content(path);
char *statuscode = "200 OK";
if (strcmp(response, "404 Not Found") == 0)
statuscode = "404 Not Found";
char *http_response = (char *)malloc(RESSIZE);
char *date = get_date_for_server();
int content_length = strlen(response);
int len, bytes_sent;
int n =
snprintf(http_response, RESSIZE,
"HTTP/1.1 %s\r\nAcess-Control-Allow-Origin: *"
"*\r\nConnection: Keep-Alive\r\nDate: %s\r\nContent-Type: "
"%s\r\nContent-Length: %d\r\n\n%s",
statuscode, date, "text/html", content_length, response);
len = strlen(http_response);
bytes_sent = send(newfd, http_response, len, 0);
shutdown(newfd, SHUT_WR);
free(date);
free(http_response);
}
} else if (strcmp(ext, ".css") == 0) {
response = content(path);
char *statuscode = "200 OK";
if (strcmp(response, "404 Not Found") == 0)
statuscode = "404 Not Found";
char *http_response = (char *)malloc(RESSIZE);
char *date = get_date_for_server();
int content_length = strlen(response);
int len, bytes_sent;
int n =
snprintf(http_response, RESSIZE,
"HTTP/1.1 %s\r\nAcess-Control-Allow-Origin: *"
"*\r\nConnection: Keep-Alive\r\nDate: %s\r\nContent-Type: "
"%s\r\nContent-Length: %d\r\n\n%s",
statuscode, date, "text/css", content_length, response);
len = strlen(http_response);
bytes_sent = send(newfd, http_response, len, 0);
shutdown(newfd, SHUT_WR);
free(date);
free(http_response);
}
else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0 ||
strcmp(ext, ".png") == 0) {
char http_header[256];
char *date = get_date_for_server();
struct file_data *fdata = NULL;
fdata = read_file(path);
if (fdata == NULL) {
fprintf(stderr, "Error opening binary file");
char *temp404 = "404 Not Found";
int templen = strlen(temp404);
char tempresponse[256];
int temp_n =
snprintf(tempresponse, 256,
"HTTP/1.1 404 Not Found\r\nAcess-Control-Allow-Origin: *"
"*\r\nConnection: Keep-Alive\r\nDate: %s\r\nContent-Type: "
"%s\r\nContent-Length: %d\r\n\n%s",
date, "text/html", templen, temp404);
int temp_bytes_sent =
send(newfd, tempresponse, strlen(tempresponse), 0);
temp404 = NULL;
}
else {
uint8_t buff[fdata->length];
snprintf(http_header, 256,
"HTTP/1.1 200 OK\r\nContent-Type: "
"image/jpeg\r\nContent-Length: %llu\r\n\r\n",
fdata->length);
send(newfd, http_header, strlen(http_header), 0);
send(newfd, fdata->data, fdata->length, 0);
shutdown(newfd, SHUT_WR);
fdata = NULL;
}
} else {
shutdown(newfd, SHUT_WR);
}
}
close(sockfd);
return 0;
}