Add very basic server

master
Nick Krichevsky 2018-09-08 17:26:34 -04:00
parent b12210e1e1
commit 262a1bd212
3 changed files with 29 additions and 3 deletions

View File

@ -1,8 +1,11 @@
#include "http_server.h"
#include "../common/socket_helper.h"
#include "socket_server.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
@ -13,4 +16,8 @@ int main(int argc, char* argv[]) {
if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) {
printf("%s", PORT_ERROR);
}
struct server_info info = setup(port_num);
serve_one_request(info.sock_fd);
shutdown(info.sock_fd, SHUT_RDWR);
close(info.sock_fd);
}

View File

@ -1,8 +1,11 @@
#include "socket_server.h"
#include "../common/http_types.h"
#include "../common/socket_helper.h"
#include <math.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
@ -57,8 +60,7 @@ struct server_info setup(int port) {
return info;
}
int listen_result = listen(info.sock_fd, 8);
if (listen_result == -1) {
int listen_result = listen(info.sock_fd, 8); if (listen_result == -1) {
close(info.sock_fd);
info.sock_fd = -1;
info.status = STATUS_ERROR;
@ -68,3 +70,18 @@ struct server_info setup(int port) {
return info;
}
enum socket_read_result serve_one_request(int sock_fd) {
int client_fd = accept(sock_fd, NULL, NULL);
if (client_fd == -1) {
return RESULT_WRITE_ERROR;
}
struct http_message message = {};
get_all_remote_parts(client_fd, TYPE_SERVER, &message);
printf("%s\n", message.contents);
char *msg = "HTTP/1.1 501 Not Imlemented Yet :(\r\n\r\n";
send(client_fd, msg, strlen(msg), 0);
shutdown(client_fd, SHUT_RDWR);
close(client_fd);
return RESULT_OK;
}

View File

@ -1,6 +1,8 @@
#ifndef SOCKET_SERVER
#define SOCKET_SERVER
#include "../common/socket_helper.h"
#define BACKLOG_SIZE 8;
enum server_status {STATUS_LISTENING, STATUS_CLOSED, STATUS_ERROR};
@ -10,6 +12,6 @@ struct server_info {
};
struct server_info setup(int port);
char *serve(struct server_info *info);
enum socket_read_result serve_one_request(int sock_fd);
#endif