Created
December 19, 2023 05:55
-
-
Save luislortega/b7b1af37ef3ccbfbbfd51f2703883742 to your computer and use it in GitHub Desktop.
Minimalist C Web Server - not for production use, only for fun :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <string.h> | |
#include <fcntl.h> | |
#include <sys/sendfile.h> | |
#include <unistd.h> | |
#include <netinet/in.h> | |
void main() { | |
int s = socket(AF_INET, SOCK_STREAM, 0); | |
struct sockaddr_in addr = { | |
AF_INET, | |
0x901f, | |
0 | |
}; | |
bind(s, &addr, sizeof(addr)); | |
listen(s, 10); | |
int client_fd = accept(s, 0, 0); | |
char buffer[256] = {0}; | |
recv(client_fd, buffer, 256, 0); | |
// GET /file.html ..... | |
char* f = buffer + 5; | |
*strchr(f, ' ') = 0; | |
int opened_fd = open(f, O_RDONLY); | |
sendfile(client_fd, opened_fd, 0, 256); | |
close(opened_fd); | |
close(client_fd); | |
close(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment