Compare commits
4 Commits
54d0b65300
...
c_rewrite
Author | SHA1 | Date | |
---|---|---|---|
9adeaa8ff9
|
|||
2e22186952
|
|||
feee7b53cc
|
|||
8ba1b04f08
|
@ -1,3 +0,0 @@
|
|||||||
target/
|
|
||||||
debug/
|
|
||||||
**/*.rs.bk
|
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
libstrops.a
|
libhttp.a
|
||||||
http_server
|
http_server
|
||||||
|
21
Dockerfile
21
Dockerfile
@ -1,21 +0,0 @@
|
|||||||
FROM gcc AS build
|
|
||||||
|
|
||||||
WORKDIR /http_server
|
|
||||||
|
|
||||||
COPY ./main.c .
|
|
||||||
COPY ./libstrops.a .
|
|
||||||
|
|
||||||
RUN gcc -I . -L. -static -ansi -O2 -o http_server main.c -lstrops
|
|
||||||
|
|
||||||
RUN strip ./http_server
|
|
||||||
|
|
||||||
FROM scratch
|
|
||||||
|
|
||||||
# copy the build artifact from the build stage
|
|
||||||
COPY --from=build /http_server /
|
|
||||||
COPY ./www /www
|
|
||||||
COPY ./internal /internal
|
|
||||||
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
CMD ["/http_server"]
|
|
8
Makefile
Normal file
8
Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
.POSIX:
|
||||||
|
server: server.c libhttp.a
|
||||||
|
gcc -I . -L. -ansi -ggdb -o http_server server.c -lstrops -Wl,-Bstatic -lhttp -Wl,-Bdynamic
|
||||||
|
|
||||||
|
libhttp.a: http.c
|
||||||
|
gcc -c -ansi -ggdb -o http.o http.c -lstrops
|
||||||
|
ar cr libhttp.a http.o
|
||||||
|
rm http.o
|
15
compose.yml
15
compose.yml
@ -1,15 +0,0 @@
|
|||||||
networks:
|
|
||||||
http-network:
|
|
||||||
name: http-network
|
|
||||||
driver: bridge
|
|
||||||
|
|
||||||
services:
|
|
||||||
http-server:
|
|
||||||
container_name: http-server
|
|
||||||
build: .
|
|
||||||
image: http-server:latest
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- 80:8080
|
|
||||||
networks:
|
|
||||||
- http-network
|
|
104
http.c
Normal file
104
http.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#include "http.h"
|
||||||
|
#include <strops.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/* TODO: https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable */
|
||||||
|
int http_init(int port, int connection_amount) {
|
||||||
|
int ret;
|
||||||
|
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (server_sock == -1) {
|
||||||
|
perror("Couldn't create socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in server_addr;
|
||||||
|
server_addr.sin_family = AF_INET;
|
||||||
|
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
server_addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof server_addr) == -1) {
|
||||||
|
perror("Couldn't bind socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(server_sock, connection_amount) == -1) {
|
||||||
|
perror("Cannot listen on socket");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return server_sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTP_Request* http_accept(int server) {
|
||||||
|
int client_sock = accept(server, NULL, NULL);
|
||||||
|
if (client_sock == -1) {
|
||||||
|
perror("Couldn't connect to client");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTP_Request *request = malloc(sizeof(HTTP_Request));
|
||||||
|
request->client_sock = client_sock;
|
||||||
|
|
||||||
|
/* TODO: Read entire message and parse into request struct */
|
||||||
|
ssize_t bytes_read;
|
||||||
|
size_t bufsize = 4096;
|
||||||
|
char buf[bufsize];
|
||||||
|
|
||||||
|
bytes_read = read(request->client_sock, buf, bufsize - 1);
|
||||||
|
if (bytes_read == -1) {
|
||||||
|
perror("Failed to read data");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: read tmp line by line */
|
||||||
|
char *tmp = strops_trim_left_string(buf, "\r\n");
|
||||||
|
size_t lines_count = 0;
|
||||||
|
size_t lines_capacity = 10;
|
||||||
|
char **lines = malloc(lines_capacity * sizeof (char*));
|
||||||
|
size_t i;
|
||||||
|
while (strops_length(tmp) > 0) {
|
||||||
|
char *line = malloc(strops_length(tmp));
|
||||||
|
memset(line, 0, strops_length(tmp));
|
||||||
|
for (i = 0; i < strops_length(tmp); i++) {
|
||||||
|
if (tmp[i] == '\r' && tmp[i + 1] == '\n') {
|
||||||
|
i += 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
line[i] = tmp[i];
|
||||||
|
}
|
||||||
|
for (; i > 0; i--) {
|
||||||
|
strops_remove_at_pos_char_inplace(tmp, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lines_count >= lines_capacity) {
|
||||||
|
lines_capacity *= 2;
|
||||||
|
lines = realloc(lines, lines_capacity * sizeof (char*));
|
||||||
|
}
|
||||||
|
lines[lines_count] = line;
|
||||||
|
lines_count++;
|
||||||
|
}
|
||||||
|
free(tmp);
|
||||||
|
/* TODO: Parse lines */
|
||||||
|
/* TODO: find suitable data structure for field-lines */
|
||||||
|
/*
|
||||||
|
request-line => method target version
|
||||||
|
seperated by whitespace (SP)
|
||||||
|
|
||||||
|
field-line => field-name:field-value
|
||||||
|
seperated by a single colon => ':'
|
||||||
|
field-value may have leading and trailing optionial whitespace (OWS)
|
||||||
|
*/
|
||||||
|
for (i = 0; i < lines_count; i++) {
|
||||||
|
printf("%s\n", lines[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: remeber to delete this code after finishing parsing code */
|
||||||
|
for (i = lines_count; i > 0; i--) {
|
||||||
|
free(lines[i]);
|
||||||
|
}
|
||||||
|
free(lines);
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
26
http.h
Normal file
26
http.h
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#ifndef HTTP_H
|
||||||
|
#define HTTP_H
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int client_sock;
|
||||||
|
char *method;
|
||||||
|
char *target;
|
||||||
|
char *version;
|
||||||
|
/* Field_Lines (Header) */
|
||||||
|
char *body;
|
||||||
|
} HTTP_Request;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *version;
|
||||||
|
unsigned int status;
|
||||||
|
/* Field_Lines (Header) */
|
||||||
|
char *body;
|
||||||
|
} HTTP_Response;
|
||||||
|
|
||||||
|
int http_init(int port, int connection_amount);
|
||||||
|
HTTP_Request* http_accept(int server);
|
||||||
|
|
||||||
|
#endif /* HTTP_H */
|
62
main.c
62
main.c
@ -1,62 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#define PORT 8080
|
|
||||||
|
|
||||||
void handle_client(int client) {
|
|
||||||
ssize_t bytes_read;
|
|
||||||
size_t bufsize = 1024;
|
|
||||||
char buf[bufsize];
|
|
||||||
|
|
||||||
bytes_read = read(client, buf, bufsize - 1);
|
|
||||||
if (bytes_read == -1) {
|
|
||||||
fprintf(stderr, "Failed to read data. Error = %s\n", strerror(errno));
|
|
||||||
}
|
|
||||||
printf("%s\n", buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
int ret;
|
|
||||||
int server = socket(AF_INET, SOCK_STREAM, 0);
|
|
||||||
if (server == -1) {
|
|
||||||
fprintf(stderr, "Couldn't create socket. Error = %s\n", strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr_in addr;
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
addr.sin_port = htons(PORT);
|
|
||||||
|
|
||||||
ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
|
|
||||||
if (ret == -1) {
|
|
||||||
fprintf(stderr, "Couldn't bind socket. Error = %s\n", strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = listen(server, 3);
|
|
||||||
if (ret == -1) {
|
|
||||||
fprintf(stderr, "Cannot listen on socket. Error = %s", strerror(errno));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int client;
|
|
||||||
while(1) {
|
|
||||||
client = accept(server, (struct sockaddr*)&addr, sizeof(addr));
|
|
||||||
if (client == -1) {
|
|
||||||
fprintf(stderr, "Couldn't connect to client. Error = %s\n", strerror(errno));
|
|
||||||
}
|
|
||||||
handle_client(client);
|
|
||||||
close(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unreacheable Code */
|
|
||||||
close(server);
|
|
||||||
return 0;
|
|
||||||
}
|
|
48
server.c
Normal file
48
server.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
#include <strops.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "http.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define PORT 8080
|
||||||
|
|
||||||
|
HTTP_Response* handle_client(HTTP_Request *request) {
|
||||||
|
ssize_t bytes_written;
|
||||||
|
|
||||||
|
char *response = "HTTP/1.1 200 \r\n\r\n";
|
||||||
|
bytes_written = write(request->client_sock, response, strops_length(response));
|
||||||
|
if (bytes_written != strops_length(response)) {
|
||||||
|
fprintf(stderr, "Incomplete write\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
TODO: implement signals
|
||||||
|
TODO: graceful server shutdown
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
int ret;
|
||||||
|
int server;
|
||||||
|
server = http_init(PORT, 3);
|
||||||
|
if (server == -1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTP_Request *request;
|
||||||
|
while(1) {
|
||||||
|
request = http_accept(server);
|
||||||
|
if (request == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
handle_client(request);
|
||||||
|
close(request->client_sock);
|
||||||
|
free(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(server);
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user