-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsrv.c
More file actions
202 lines (171 loc) · 6.71 KB
/
httpsrv.c
File metadata and controls
202 lines (171 loc) · 6.71 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
/*
* httpsrv.c - minimal HTTP Server
*
* Copyright (C) 2026 Raphael Hoefler
*
* This program 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.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUF_SIZE 4096
// ---- Content-Type erkennen nach Dateiendung ----
const char* get_content_type(const char *path) {
const char *dot = strrchr(path, '.');
if (!dot) return "application/octet-stream";
if (strcmp(dot, ".html") == 0) return "text/html";
if (strcmp(dot, ".htm") == 0) return "text/html";
if (strcmp(dot, ".txt") == 0) return "text/plain";
if (strcmp(dot, ".css") == 0) return "text/css";
if (strcmp(dot, ".js") == 0) return "application/javascript";
if (strcmp(dot, ".png") == 0) return "image/png";
if (strcmp(dot, ".jpg") == 0) return "image/jpeg";
if (strcmp(dot, ".jpeg") == 0) return "image/jpeg";
if (strcmp(dot, ".gif") == 0) return "image/gif";
if (strcmp(dot, ".ico") == 0) return "image/x-icon";
return "application/octet-stream";
}
int main(int argc, char *argv[]) {
int server_fd, client_fd;
struct sockaddr_in address;
int opt = 1;
socklen_t addrlen = sizeof(address);
char httproot[1024] = ".";
int port = 8090;
// ---- Kommandozeilenargumente ----
int optch;
while ((optch = getopt(argc, argv, "r:p:")) != -1) {
switch (optch) {
case 'r': strncpy(httproot, optarg, sizeof(httproot)-1); break;
case 'p': port = atoi(optarg); break;
default:
fprintf(stderr, "Usage: %s [-r httproot] [-p port]\n", argv[0]);
exit(1);
}
}
printf("HTTP Root: %s\n", httproot);
printf("Port: %d\n", port);
// ---- Socket setup ----
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) { perror("socket"); exit(1); }
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind"); exit(1);
}
if (listen(server_fd, 10) < 0) {
perror("listen"); exit(1);
}
printf("Listening on port %d...\n", port);
// ---- Server loop ----
while (1) {
char buffer[BUF_SIZE];
int total = 0;
client_fd = accept(server_fd, (struct sockaddr*)&address, &addrlen);
if (client_fd < 0) { perror("accept"); continue; }
// ---- HTTP Header lesen ----
while (1) {
int n = read(client_fd, buffer + total, sizeof(buffer) - total - 1);
if (n <= 0) break;
total += n;
buffer[total] = 0;
if (strstr(buffer, "\r\n\r\n")) break; // Ende Header
}
// ---- Erste Zeile parsen ----
char *saveptr;
char *line = strtok_r(buffer, "\r\n", &saveptr);
char method[16];
char path[1024] = "/";
if (line) {
sscanf(line, "%15s %1023s", method, path);
}
printf("Requested path: %s\n", path);
// ---- User-Agent extrahieren ----
char useragent[1024] = "(none)";
line = strtok_r(NULL, "\r\n", &saveptr);
while (line) {
if (strncasecmp(line, "User-Agent:", 11) == 0) {
char *ua = line + 11;
while (*ua == ' ') ua++;
strncpy(useragent, ua, sizeof(useragent) - 1);
useragent[sizeof(useragent) - 1] = 0;
break;
}
line = strtok_r(NULL, "\r\n", &saveptr);
}
char response[BUF_SIZE];
// ---- /useragent Sonderfall ----
if (strcmp(path, "/useragent") == 0) {
snprintf(response, sizeof(response),
"HTTP/1.1 200 OK\r\n"
"Server: httpsrv\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %zu\r\n"
"Connection: close\r\n"
"\r\n"
"%s",
strlen(useragent), useragent);
send(client_fd, response, strlen(response), 0);
} else {
// ---- Datei aus HTTP-Root laden ----
char filepath[2048];
// "/" → "/index.html"
if (strcmp(path, "/") == 0) {
snprintf(filepath, sizeof(filepath), "%s/index.html", httproot);
} else {
snprintf(filepath, sizeof(filepath), "%s%s", httproot, path);
}
struct stat st;
if (stat(filepath, &st) == 0 && S_ISREG(st.st_mode)) {
int fd = open(filepath, O_RDONLY);
if (fd < 0) {
snprintf(response, sizeof(response),
"HTTP/1.1 500 Internal Server Error\r\n"
"Connection: close\r\n\r\n");
send(client_fd, response, strlen(response), 0);
} else {
char filebuf[BUF_SIZE];
int n = read(fd, filebuf, sizeof(filebuf));
close(fd);
snprintf(response, sizeof(response),
"HTTP/1.1 200 OK\r\n"
"Server: httpsrv\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n",
get_content_type(filepath), n);
send(client_fd, response, strlen(response), 0);
send(client_fd, filebuf, n, 0);
}
} else {
snprintf(response, sizeof(response),
"HTTP/1.1 404 Not Found\r\n"
"Connection: close\r\n\r\n");
send(client_fd, response, strlen(response), 0);
}
}
shutdown(client_fd, SHUT_WR);
close(client_fd);
}
}