More changes

This commit is contained in:
2014-08-15 21:33:24 +01:00
parent 05ba770d13
commit a702315531
28 changed files with 238 additions and 114 deletions

View File

@@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define _UNUSED_
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

View File

@@ -47,6 +47,7 @@ OBJECTFILES= \
${OBJECTDIR}/src/main.o \
${OBJECTDIR}/src/mime.o \
${OBJECTDIR}/src/queue.o \
${OBJECTDIR}/src/server-connection.o \
${OBJECTDIR}/src/server-socket.o \
${OBJECTDIR}/src/socket.o \
${OBJECTDIR}/src/thread-pool.o \
@@ -137,6 +138,11 @@ ${OBJECTDIR}/src/queue.o: nbproject/Makefile-${CND_CONF}.mk src/queue.c
${RM} "$@.d"
$(COMPILE.c) -g -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/queue.o src/queue.c
${OBJECTDIR}/src/server-connection.o: nbproject/Makefile-${CND_CONF}.mk src/server-connection.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
$(COMPILE.c) -g -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/server-connection.o src/server-connection.c
${OBJECTDIR}/src/server-socket.o: nbproject/Makefile-${CND_CONF}.mk src/server-socket.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"

View File

@@ -47,6 +47,7 @@ OBJECTFILES= \
${OBJECTDIR}/src/main.o \
${OBJECTDIR}/src/mime.o \
${OBJECTDIR}/src/queue.o \
${OBJECTDIR}/src/server-connection.o \
${OBJECTDIR}/src/server-socket.o \
${OBJECTDIR}/src/socket.o \
${OBJECTDIR}/src/thread-pool.o \
@@ -137,6 +138,11 @@ ${OBJECTDIR}/src/queue.o: nbproject/Makefile-${CND_CONF}.mk src/queue.c
${RM} "$@.d"
$(COMPILE.c) -O2 -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/queue.o src/queue.c
${OBJECTDIR}/src/server-connection.o: nbproject/Makefile-${CND_CONF}.mk src/server-connection.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
$(COMPILE.c) -O2 -Werror -DINI_ALLOW_BOM=0 -DINI_ALLOW_MULTILINE=0 -D_GNU_SOURCE -Ilib -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/server-connection.o src/server-connection.c
${OBJECTDIR}/src/server-socket.o: nbproject/Makefile-${CND_CONF}.mk src/server-socket.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"

View File

@@ -11,15 +11,18 @@
<itemPath>src/http.h</itemPath>
<itemPath>lib/http_parser.h</itemPath>
<itemPath>lib/ini.h</itemPath>
<itemPath>src/khttp.h</itemPath>
<itemPath>src/log.h</itemPath>
<itemPath>src/main-loop.h</itemPath>
<itemPath>src/main.h</itemPath>
<itemPath>src/mime.h</itemPath>
<itemPath>src/queue.h</itemPath>
<itemPath>src/server-connection.h</itemPath>
<itemPath>src/server-socket.h</itemPath>
<itemPath>src/socket.h</itemPath>
<itemPath>src/thread-pool.h</itemPath>
<itemPath>src/util.h</itemPath>
<itemPath>src/version.h</itemPath>
</logicalFolder>
<logicalFolder name="ResourceFiles"
displayName="Resource Files"
@@ -40,6 +43,7 @@
<itemPath>src/main.c</itemPath>
<itemPath>src/mime.c</itemPath>
<itemPath>src/queue.c</itemPath>
<itemPath>src/server-connection.c</itemPath>
<itemPath>src/server-socket.c</itemPath>
<itemPath>src/socket.c</itemPath>
<itemPath>src/thread-pool.c</itemPath>
@@ -127,6 +131,8 @@
</item>
<item path="src/http.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/khttp.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/log.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/log.h" ex="false" tool="3" flavor2="0">
@@ -147,6 +153,10 @@
</item>
<item path="src/queue.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/server-connection.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/server-connection.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/server-socket.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/server-socket.h" ex="false" tool="3" flavor2="0">
@@ -163,6 +173,8 @@
</item>
<item path="src/util.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/version.h" ex="false" tool="3" flavor2="0">
</item>
</conf>
<conf name="Release" type="1">
<toolsSet>
@@ -229,6 +241,8 @@
</item>
<item path="src/http.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/khttp.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/log.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/log.h" ex="false" tool="3" flavor2="0">
@@ -249,6 +263,10 @@
</item>
<item path="src/queue.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/server-connection.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/server-connection.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/server-socket.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/server-socket.h" ex="false" tool="3" flavor2="0">
@@ -265,6 +283,8 @@
</item>
<item path="src/util.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/version.h" ex="false" tool="3" flavor2="0">
</item>
</conf>
</confs>
</configurationDescriptor>

View File

@@ -5,9 +5,10 @@
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include "main.h"
#include "config.h"
#include "util.h"
#include "ini.h"
const char *default_servername = "localhost";

View File

@@ -8,11 +8,13 @@
#ifndef CONFIG_H
#define CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "main.h"
#define CONFIG_SERVER_FOREACH_HOST(config, elem) \
elem = config->hosts != NULL ? config->hosts[0] : NULL; \
@@ -32,7 +34,6 @@ extern "C" {
char *servername;
char *administrator;
uint16_t listen_port;
config_host **hosts;
size_t host_count;
} config_server;

View File

@@ -6,7 +6,6 @@
#include <string.h>
#include "ut/utlist.h"
#include "data-buffer.h"
data_buffer_list* data_buffer_list_new() {
@@ -31,7 +30,7 @@ void data_buffer_list_delete(data_buffer_list *list) {
free(list->rdlock);
free(list);
}
int data_buffer_list_append(data_buffer_list *list, const char* src, size_t n) {
void data_buffer_list_append(data_buffer_list *list, const char* src, size_t n) {
assert(list!=NULL);
assert(src!=NULL && n>0);
BUFFER_LIST_WR_LOCK(list);

View File

@@ -36,7 +36,7 @@ extern "C" {
data_buffer_list* data_buffer_list_new();
void data_buffer_list_delete(data_buffer_list *list);
int data_buffer_list_append(data_buffer_list *list, const char* src, size_t n);
void data_buffer_list_append(data_buffer_list *list, const char* src, size_t n);
void data_buffer_list_lock(data_buffer_list *list, bool rd, bool wr);
void data_buffer_list_unlock(data_buffer_list *list, bool rd, bool wr);

View File

@@ -3,21 +3,20 @@
#include <stdbool.h>
#include <assert.h>
#include "main.h"
#include "util.h"
#include "http.h"
#include "http_parser.h"
#include "server-connection.h"
#include "http-reader.h"
#include "main-loop.h"
#define GET_CB_STR(str, at, length) do { \
str = calloc(length+1, sizeof(char));\
strncpy(str, at, length);\
}while(0);
#define SKT(parser) ((hmain_parse_data*)parser->data)
#define SKT(parser) ((request_parse_state*)parser->data)
http_parser_settings *parser_settings = NULL;
http_parser_settings* parser_get_settings(hmain_parse_data *data) {
http_parser_settings* parser_get_settings() {
if (parser_settings == NULL) {
parser_settings = calloc(1, sizeof(http_parser_settings));
parser_settings->on_body = parser_cb_on_body;
@@ -28,6 +27,7 @@ http_parser_settings* parser_get_settings(hmain_parse_data *data) {
parser_settings->on_message_complete = parser_cb_on_message_complete;
parser_settings->on_status = parser_cb_on_status;
parser_settings->on_url = parser_cb_on_url;
}
return parser_settings;
}

View File

@@ -11,13 +11,21 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "http_parser.h"
#include "http.h"
#include "main.h"
#include "main-loop.h"
http_parser_settings* parser_get_settings(hmain_parse_data *elem);
#include <stddef.h>
#include "http.h"
#include "http_parser.h"
typedef struct request_parse_state {
http_request *current_request;
bool request_complete;
struct http_parser *parser;
http_header *parser_current_header;
enum {HSTATE_NONE, HSTATE_VALUE, HSTATE_FIELD} parser_header_state;
} request_parse_state;
http_parser_settings* parser_get_settings();
void parser_free_settings();
int parser_cb_on_message_begin(http_parser* parser);

View File

@@ -9,13 +9,14 @@
#include <dirent.h>
#include <time.h>
#include "ut/utstring.h"
#include "http_parser.h"
#include "http.h"
#include "main.h"
#include "config.h"
#include "http-server.h"
#include "mime.h"
#include "ut/utstring.h"
#include "util.h"
http_response* server_process_request(config_server* config, http_request *request) {
http_response* response = NULL;

View File

@@ -12,12 +12,12 @@
extern "C" {
#endif
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <dirent.h>
#include "http.h"
#include "main.h"
#include "config.h"
typedef struct server_file_result {
bool error;

View File

@@ -4,10 +4,12 @@
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include "main.h"
#include "ut/utarray.h"
#include "ut/utstring.h"
#include "http.h"
#include "util.h"
#include "version.h"
void http_header_icd_init_f(void* elem) {
memset(elem, 1, sizeof(http_header));

View File

@@ -8,31 +8,33 @@
#ifndef HTTP_H
#define HTTP_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "ut/utarray.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
#include "ut/utarray.h"
#define HEADER_CONTENT_TYPE "Content-Type"
#define HEADER_CONTENT_LENGTH "Content-Length"
#define HEADER_USER_AGENT "User-Agent"
#define HEADER_SERVER "Server"
#define HEADER_LAST_MODIFIED "Last-Modified"
#define HEADER_LOCATION "Location"
#define HEADER_HOST "Host"
#define HEADER_TRANSFER_ENCODING "Transfer-Encoding"
#define HEADER_DATE "Date"
#define HEADER_CONNECTION "Connection"
#define HEADER_IF_MODIFIED_SINCE "If-Modified-Since"
#define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since"
#define FORMAT_HEADER_DATE "%a, %d %h %Y %T %Z"
#define DEFAULT_CONTENT_TYPE "text/plain"
#define HTTP_CHUNK_MAXSIZE 1024*16
#define HEADER_CONTENT_TYPE "Content-Type"
#define HEADER_CONTENT_LENGTH "Content-Length"
#define HEADER_USER_AGENT "User-Agent"
#define HEADER_SERVER "Server"
#define HEADER_LAST_MODIFIED "Last-Modified"
#define HEADER_LOCATION "Location"
#define HEADER_HOST "Host"
#define HEADER_TRANSFER_ENCODING "Transfer-Encoding"
#define HEADER_DATE "Date"
#define HEADER_CONNECTION "Connection"
#define HEADER_IF_MODIFIED_SINCE "If-Modified-Since"
#define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since"
#define FORMAT_HEADER_DATE "%a, %d %h %Y %T %Z"
#define DEFAULT_CONTENT_TYPE "text/plain"
#define HTTP_CHUNK_MAXSIZE 1024*16
typedef enum http_request_method {
METHOD_GET, METHOD_POST, METHOD_HEAD, METHOD_PUT,

View File

@@ -8,16 +8,18 @@
#include <errno.h>
#include <unistd.h>
#include "main-loop.h"
#include "mime.h"
#include "log.h"
#include "socket.h"
#include "thread-pool.h"
#include "queue.h"
#include "log.h"
#include "http_parser.h"
#include "http.h"
#include "http-reader.h"
#include "server-socket.h"
#include "util.h"
#include "config.h"
#include "main-loop.h"
hmain_connection* hmain_connection_new(int fd, hmain_status *status) {
static u_int64_t nextid = 1;
@@ -121,12 +123,10 @@ void hmain_setup(hmain_status *status) {
status->pools[POOL_WORKERS] = pool;
thread_pool_start(pool);
}
void hmain_teardown(hmain_status *status) {
if (status == NULL) {
fatal("hmain is not setup");
}
assert(status!=NULL);
//Stop pools
size_t pool_count = sizeof(status->pools) / sizeof(status->pools[0]);
for(int i=0; i<pool_count; i++) {
@@ -238,7 +238,6 @@ void* thloop_read(void * arg) {
}
hmain_connection *conn = (hmain_connection*)item->data;
CONN_LOCK(conn);
conn->isReading = true;
char buffer[16*1024];
int count;
@@ -269,7 +268,6 @@ void* thloop_read(void * arg) {
}
*/
} while(count > 0);
conn->isReading = false;
CONN_UNLOCK(conn);
}
}
@@ -284,11 +282,10 @@ void* thloop_write(void * arg) {
hmain_connection *conn = (hmain_connection*)item->data;
do {
CONN_LOCK(conn);
conn->isWriting = true;
}while(0);
conn->isWriting = false;
CONN_UNLOCK(conn);
}
}

View File

@@ -8,20 +8,13 @@
#ifndef MAIN_LOOP_H
#define MAIN_LOOP_H
#include <stdbool.h>
#include <time.h>
#include <pthread.h>
#include "http_parser.h"
#include "config.h"
#include "thread-pool.h"
#include "http.h"
#include "data-buffer.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <time.h>
#include <pthread.h>
#define EPOLL_MAXEVENTS 128
@@ -32,7 +25,7 @@ extern "C" {
POOL_READ, POOL_WRITE, POOL_WORKERS, POOL_DISK_READ
} hmain_pool;
typedef enum skt_elem_hstate {HSTATE_NONE, HSTATE_VALUE, HSTATE_FIELD} skt_elem_hstate;
//typedef enum skt_elem_hstate {HSTATE_NONE, HSTATE_VALUE, HSTATE_FIELD} skt_elem_hstate;
typedef struct hmain_connection {
uint64_t cid;
@@ -64,7 +57,7 @@ extern "C" {
bool request_complete;
http_parser *parser;
http_header *parser_current_header;
skt_elem_hstate parser_header_state;
int parser_header_state;
} hmain_parse_data;
hmain_connection* hmain_connection_new(int fd, hmain_status *status);

View File

@@ -17,38 +17,10 @@
#include <signal.h>
#include <bits/stdio2.h>
#include "http_parser.h"
#include "ut/utlist.h"
#include "ut/utarray.h"
#include "main.h"
#include "socket.h"
#include "http.h"
#include "http-reader.h"
#include "config.h"
#include "http-server.h"
#include "mime.h"
#include "queue.h"
#include "thread-pool.h"
#include "log.h"
#include "main-loop.h"
int serverfd = 0;
volatile static bool stop = false;
static void signal_int(int signum) {
fprintf(stderr, "Terminating...\n");
stop = true;
}
int main(int argc, char** argv) {
hmain_status *status = calloc(1, sizeof(hmain_status));
hmain_setup(status);
hmain_loop(status);
hmain_teardown(status);
return (EXIT_SUCCESS);
}

View File

@@ -11,12 +11,6 @@
#ifdef __cplusplus
extern "C" {
#endif
#define SERVER_NAME "KHTTP/0.1"
#include <stdbool.h>
#include "http.h"
#include "util.h"
int main(int argc, char** argv);

View File

@@ -3,9 +3,9 @@
#include <pthread.h>
#include <string.h>
#include "queue.h"
#include "util.h"
#include "ut/utlist.h"
#include "queue.h"
queue_item* queue_item_new() {
static uint64_t nextid = 0;
@@ -18,7 +18,7 @@ queue_item* queue_item_new() {
queue_item* queue_item_new2(char* tag, void* data) {
queue_item *item = queue_item_new();
item->tag[0] = '\0';
strncat(item->tag, tag, (sizeof(item->tag)/sizeof(char))+1);
strncat(item->tag, tag, (sizeof(item->tag)/sizeof(char))-1);
item->data = data;
return item;
}
@@ -65,16 +65,35 @@ int queue_add(queue *q, queue_item *item) {
}
int queue_remove(queue *q, queue_item *item) {
QUEUE_LOCK(q);
int result = 0;
queue_item *elem, *tmp;
DL_FOREACH_SAFE(q->list, elem, tmp) {
if (elem == item) {
DL_DELETE(q->list, elem);
queue_item_delete(elem);
result = 1;
break;
}
}
q->count--;
q->count -= result;
QUEUE_UNLOCK(q);
return result;
}
int queue_remove_byptr(queue *q, void* ptr) {
QUEUE_LOCK(q);
int result = 0;
queue_item *elem, *tmp;
DL_FOREACH_SAFE(q->list, elem, tmp) {
if (elem->data == ptr) {
DL_DELETE(q->list, elem);
queue_item_delete(elem);
result ++;
}
}
q->count -= result;
QUEUE_UNLOCK(q);
return result;
}
queue_item* queue_fetchone(queue *q, bool blocking) {
queue_item *item = NULL;
@@ -91,8 +110,7 @@ queue_item* queue_fetchone(queue *q, bool blocking) {
}
}
if (item != NULL) {
item = q->list;
DL_DELETE(q->list, q->list);
DL_DELETE(q->list, item);
q->count--;
}
}

View File

@@ -53,6 +53,7 @@ extern "C" {
void queue_delete(queue *q);
int queue_add(queue *q, queue_item *item);
int queue_remove(queue *q, queue_item *item);
int queue_remove_byptr(queue *q, void* ptr);
queue_item* queue_fetchone(queue *q, bool blocking);
void queue_unblock(queue *q, uint64_t itemid);
void queue_clear(queue *q);

33
src/server-connection.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include "http.h"
#include "server-connection.h"
server_connection* server_connection_new(skt_info *skt) {
static uint64_t nextid = 1;
assert(skt!=null);
server_connection *conn = calloc(1, sizeof(server_connection));
conn->id = __atomic_fetch_add(&nextid, 1, __ATOMIC_SEQ_CST);
conn->last_activity = time(NULL);
conn->parse_state = NULL;
conn->pending_responses = http_response_list_new();
conn->pending_writes = data_buffer_list_new();
conn->skt = skt;
conn->write_qid = 0;
return conn;
}
void server_connection_delete(server_connection *conn) {
assert(conn!=NULL);
http_response_list_delete(conn->pending_responses);
data_buffer_list_delete(conn->pending_writes);
skt_delete(conn->skt);
free(conn);
}

42
src/server-connection.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* File: server-connection.h
* Author: sam
*
* Created on 15 August 2014, 18:07
*/
#ifndef SERVER_CONNECTION_H
#define SERVER_CONNECTION_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <time.h>
#include "data-buffer.h"
#include "http.h"
#include "http-reader.h"
#include "socket.h"
typedef struct server_connection {
uint64_t id;
struct skt_info *skt;
time_t last_activity;
http_response_list *pending_responses;
data_buffer_list *pending_writes;
uint64_t write_qid;//item id in write queue
request_parse_state *parse_state;
struct server_connection *next;
} server_connection;
server_connection* server_connection_new(skt_info *skt);
void server_connection_delete(server_connection *conn);
#ifdef __cplusplus
}
#endif
#endif /* SERVER_CONNECTION_H */

View File

@@ -9,10 +9,11 @@
#include <arpa/inet.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include "socket.h"
#include "server-socket.h"
#include "main.h"
#include "util.h"
int server_socket_create() {
int fd = 0;

View File

@@ -8,12 +8,14 @@
#ifndef SERVER_SOCKET_H
#define SERVER_SOCKET_H
#include "socket.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "socket.h"
int server_socket_create();
void server_socket_listen(int fd, uint16_t port);
void server_socket_release(int fd);

View File

@@ -12,14 +12,15 @@
#include <fcntl.h>
#include <assert.h>
#include "socket.h"
#include "ut/utstring.h"
#include "data-buffer.h"
#include "main.h"
#include "ut/utlist.h"
#include "util.h"
#include "data-buffer.h"
#include "socket.h"
u_int64_t skt_nextid() {
static u_int64_t id = 0;
static u_int64_t id = 1;
return __atomic_fetch_add(&id, 1, __ATOMIC_SEQ_CST);
}
skt_info* skt_new(int fd) {

View File

@@ -13,12 +13,11 @@ extern "C" {
#endif
#include <stdbool.h>
#include <sys/types.h>
#include <stddef.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include "http.h"
#include "ut/utstring.h"
#include "data-buffer.h"
typedef struct skt_info {

View File

@@ -14,6 +14,7 @@ extern "C" {
#include <pthread.h>
#include <stdbool.h>
#include "queue.h"
typedef struct thread {

23
src/version.h Normal file
View File

@@ -0,0 +1,23 @@
/*
* File: version.h
* Author: sam
*
* Created on 15 August 2014, 19:05
*/
#ifndef VERSION_H
#define VERSION_H
#ifdef __cplusplus
extern "C" {
#endif
#define SERVER_NAME "KHTTP/0.1"
#ifdef __cplusplus
}
#endif
#endif /* VERSION_H */