-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.c
More file actions
84 lines (80 loc) · 3.26 KB
/
Copy pathhttp.c
File metadata and controls
84 lines (80 loc) · 3.26 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
/******************************************************************************
* This file is part of The Ekans Webserver. *
* Copyright (C) 2011 Samuel C. Payson *
* *
* Ekans is free software: you can redistribute it and/or modify it under the *
* terms of the GNU General Public License as published by the Free Software *
* Foundation, either version 3 of the License, or (at your option) any later *
* version. *
* *
* Ekans is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
* details. *
* *
* You should have received a copy of the GNU General Public License along *
* with Ekans. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "http.h"
#include "html_prefab.h"
#include "tcp.h"
static const char http_ok[] = "HTTP/1.0 200 OK\r\n";
void http_serve_static(SOCK client, char ** hdr, const char * file_name) {
char buffer[512];
int fd, rv;
/* We use MSG_NOSIGNAL because we don't want SIGPIPE crashing the whole *
* server. */
if ((fd = open(file_name, O_RDONLY)) == -1) {
send(client, html_404_page, html_404_length, MSG_NOSIGNAL);
} else {
send(client, http_ok, sizeof http_ok - 1, MSG_NOSIGNAL);
for (; *hdr != NULL; ++hdr) {
send(client, *hdr, strlen(*hdr), MSG_NOSIGNAL);
}
send(client, "\r\n", 2, MSG_NOSIGNAL);
while ((rv = read(fd, buffer, 512)) > 0) {
send(client, buffer, rv, MSG_NOSIGNAL);
}
if (rv == -1) {
fprintf(stderr, "Failed to read from '%s': error: %s\n", file_name,
strerror(errno));
}
if (close(fd) == -1) {
fprintf(stderr, "Failed to close '%s': error: %s\n", file_name,
strerror(errno));
}
}
if (close(client) == -1) {
fprintf(stderr, "Failed to close client socket: error: %s\n",
strerror(errno));
}
}
char * http_content_type_hdr(http_res_type res_type) {
switch (res_type) {
case RES_HTML:
case RES_HPYTHON:
case RES_PYTHON:
return "Content-Type: text/html\r\n";
case RES_CSS:
return "Content-Type: text/css\r\n";
case RES_JAVASCRIPT:
return "Content-Type: text/javascript\r\n";
case RES_JPEG:
return "Content-Type: image/jpeg\r\n";
case RES_GIF:
return "Content-Type: image/gif\r\n";
case RES_PNG:
return "Content-Type: image/png\r\n";
case RES_TXT:
return "Content-Type: text/plain\r\n";
case RES_UNKNOWN:
default:
return "Content-Type: application/octet-stream\r\n";
}
}