Add HTTP server
This commit is contained in:
124
http.c
124
http.c
@@ -5,12 +5,14 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include "http.h"
|
||||
|
||||
#define BUFSIZE 256
|
||||
|
||||
int http_listen(int listen_port, int maxcons)
|
||||
static int http_listen(int listen_port, int maxcons)
|
||||
{
|
||||
struct sockaddr_in server = {0};
|
||||
int listenfd = -1;
|
||||
@@ -22,14 +24,20 @@ int http_listen(int listen_port, int maxcons)
|
||||
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (listenfd < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int optval = 1;
|
||||
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int));
|
||||
|
||||
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
perror("bind");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (listen(listenfd, maxcons) < 0) {
|
||||
perror("listen");
|
||||
goto error;
|
||||
}
|
||||
|
||||
@@ -40,47 +48,99 @@ error:
|
||||
return -1;
|
||||
}
|
||||
|
||||
void http_process(int fd, struct sockaddr_in *addr)
|
||||
static void http_process(http_worker_t *worker, FILE *stream)
|
||||
{
|
||||
FILE *stream = fdopen(fd, "r+");
|
||||
if (!stream) {
|
||||
// Read headers
|
||||
if (!fgets(worker->client_method, sizeof(worker->client_method), stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
char line[BUFSIZE];
|
||||
fgets(line, BUFSIZE, stream);
|
||||
|
||||
fprintf(stream, "HTTP/1.1 200 OK\n");
|
||||
fprintf(stream, "Content-Type: text/plain\n");
|
||||
fprintf(stream, "\r\n");
|
||||
fprintf(stream, "Text.\n");
|
||||
fflush(stream);
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
int http_worker(int listenfd)
|
||||
{
|
||||
while (1) {
|
||||
struct sockaddr_in addr;
|
||||
int addrlen = sizeof(addr);
|
||||
int ret = accept(listenfd, (struct sockaddr *)&addr, &addrlen);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
http_process(ret, &addr);
|
||||
close(ret);
|
||||
// Consume headers
|
||||
for(int i = 0; i < 50; i++) {
|
||||
char line[BUFSIZE];
|
||||
if (!fgets(line, BUFSIZE, stream))
|
||||
return;
|
||||
if (line[0] == '\r' && line[1] == '\n')
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; worker->methods[i].name; i++) {
|
||||
if (strstr(worker->client_method, worker->methods[i].name)) {
|
||||
worker->methods[i].func(worker, stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
http_404(worker, stream);
|
||||
}
|
||||
|
||||
static void http_client(http_worker_t *worker)
|
||||
{
|
||||
worker->client_host = inet_ntoa(worker->client_addr.sin_addr);
|
||||
printf("Client connected %s.\n", worker->client_host);
|
||||
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 3;
|
||||
tv.tv_usec = 0;
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv));
|
||||
|
||||
int on = 1;
|
||||
setsockopt(worker->client_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on));
|
||||
setsockopt(worker->client_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
|
||||
|
||||
FILE *stream = fdopen(worker->client_fd, "r+");
|
||||
if (stream) {
|
||||
http_process(worker, stream);
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
close(worker->client_fd);
|
||||
worker->client_fd = -1;
|
||||
|
||||
printf("Client disconnected %s.\n", worker->client_host);
|
||||
worker->client_host = NULL;
|
||||
}
|
||||
|
||||
static int http_worker(http_worker_t *worker)
|
||||
{
|
||||
printf("http_worker=%d\n", worker->listen_fd);
|
||||
|
||||
while (1) {
|
||||
int addrlen = sizeof(worker->client_addr);
|
||||
worker->client_fd = accept(worker->listen_fd, (struct sockaddr *)&worker->client_addr, &addrlen);
|
||||
if (worker->client_fd < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
http_client(worker);
|
||||
}
|
||||
|
||||
error:
|
||||
free(worker->name);
|
||||
free(worker);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int http_worker_threads(int listenfd, int nthreads)
|
||||
int http_server(int listen_port, int maxcons, http_method_t *methods)
|
||||
{
|
||||
while (nthreads-- > 0) {
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, (void *(*)(void*))http_worker, (void*)listenfd);
|
||||
int listen_fd = http_listen(9092, maxcons);
|
||||
if (listen_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
while (maxcons-- > 0) {
|
||||
char name[20];
|
||||
sprintf(name, "HTTP%d/%d", listen_port, maxcons);
|
||||
|
||||
http_worker_t *worker = calloc(1, sizeof(http_worker_t));
|
||||
worker->name = strdup(name);
|
||||
worker->listen_fd = listen_fd;
|
||||
worker->methods = methods;
|
||||
worker->client_fd = -1;
|
||||
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
|
||||
}
|
||||
|
||||
return listen_fd;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user