Add basic server argparsing

master
Nick Krichevsky 2018-09-03 18:54:02 -04:00
parent fccf0db890
commit 5e4275ac5b
5 changed files with 26 additions and 2 deletions

View File

@ -1,5 +1,6 @@
#include "http_client.h"
#include "../common/http_types.h"
#include "../common/socket_helper.h"
#include "http_socket.h"
#include <stdbool.h>
#include <stdio.h>

View File

@ -6,8 +6,6 @@
#include <time.h>
#define HTTP_VERSION "HTTP/1.1"
#define MIN_PORT 1
#define MAX_PORT 65535
struct response_info {
char *contents;

View File

@ -3,6 +3,8 @@
#include "http_types.h"
#define MIN_PORT 1
#define MAX_PORT 65535
#define BUFFER_SIZE 1024
#define HEADER_CONTENT_LENGTH "Content-Length"
#define HEADER_TRANSFER_ENCODING "Transfer-Encoding"

16
server/http_server.c Normal file
View File

@ -0,0 +1,16 @@
#include "http_server.h"
#include "../common/socket_helper.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("%s", USAGE_STRING);
}
char *port = argv[1];
long port_num = strtol(port, NULL, 10);
if (port_num < MIN_PORT || port_num > MAX_PORT || errno == ERANGE || errno == EINVAL) {
printf("%s", PORT_ERROR);
}
}

7
server/http_server.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef HTTP_SERVER_H
#define HTTP_SERVER_H
#define USAGE_STRING "Usage: ./http_server port"
#define PORT_ERROR "Port must be a number in the range 1-65535"
#endif