aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/net')
-rw-r--r--src/net/lua.c18
-rw-r--r--src/net/luai.c232
-rw-r--r--src/net/luai.h2
-rw-r--r--src/net/util.c30
-rw-r--r--src/net/util.h8
5 files changed, 145 insertions, 145 deletions
diff --git a/src/net/lua.c b/src/net/lua.c
index 2e46943..ab112fe 100644
--- a/src/net/lua.c
+++ b/src/net/lua.c
@@ -10,7 +10,7 @@ int l_write(lua_State* L){
lua_gettable(L, -2);
int head = strcmp(luaL_checkstring(L, -1), "HEAD") == 0;
-
+
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
@@ -20,7 +20,7 @@ int l_write(lua_State* L){
size_t len;
char* content = (char*)luaL_checklstring(L, 2, &len);
-
+
lua_pushvalue(L, res_idx);
lua_pushstring(L, "header");
lua_gettable(L, -2);
@@ -54,7 +54,7 @@ int l_send(lua_State* L){
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
-
+
client_fd_errors(client_fd);
size_t len;
@@ -76,7 +76,7 @@ int l_send(lua_State* L){
i_write_header(L, header, &resp, content, len);
send(client_fd, resp->c, resp->len, MSG_NOSIGNAL);
-
+
//
lua_pushstring(L, "client_fd");
lua_pushinteger(L, -1);
@@ -89,7 +89,7 @@ int l_send(lua_State* L){
int l_close(lua_State* L){
int res_idx = 1;
-
+
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
@@ -143,7 +143,7 @@ int l_roll(lua_State* L){
lua_gettable(L, 1);
int client_fd = luaL_checkinteger(L, -1);
client_fd_errors(client_fd);
-
+
fd_set rfd;
FD_ZERO(&rfd);
FD_SET(client_fd, &rfd);
@@ -209,7 +209,7 @@ int l_sendfile(lua_State* L){
lua_gettable(L, 3);
if(!lua_isnil(L, -1)) filename = (char*)lua_tostring(L, -1);
}
-
+
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
@@ -236,7 +236,7 @@ int l_sendfile(lua_State* L){
char* content_type = map_get(mime_type, ext + 1);
if(content_type)
- {luaI_tsets(L, header, "Content-Type", content_type);}
+ {luaI_tsets(L, header, "Content-Type", content_type);}
}
char* buffer = calloc(sizeof* buffer, bsize + 1);
@@ -244,7 +244,7 @@ int l_sendfile(lua_State* L){
fseek(fp, 0L, SEEK_END);
size_t sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
-
+
char size[256];
sprintf(size, "%li", sz);
luaI_tsets(L, header, "Content-Length", size);
diff --git a/src/net/luai.c b/src/net/luai.c
index ac9e090..0b63d18 100644
--- a/src/net/luai.c
+++ b/src/net/luai.c
@@ -8,23 +8,23 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si
str* header_vs = str_init("");
lua_pushnil(L);
-
+
for(;lua_next(L, header_top) != 0;){
- char* key = (char*)luaL_checklstring(L, -2, NULL);
- if(strcmp(key, "Code") != 0){
- str_push(header_vs, key);
- str_push(header_vs, ": ");
- str_push(header_vs, (char*)luaL_checklstring(L, -1, NULL));
- str_push(header_vs, "\r\n");
- }
- lua_pop(L, 1);
+ char* key = (char*)luaL_checklstring(L, -2, NULL);
+ if(strcmp(key, "Code") != 0){
+ str_push(header_vs, key);
+ str_push(header_vs, ": ");
+ str_push(header_vs, (char*)luaL_checklstring(L, -1, NULL));
+ str_push(header_vs, "\r\n");
+ }
+ lua_pop(L, 1);
}
lua_pushvalue(L, header_top);
lua_pushstring(L, "Code");
lua_gettable(L, header_top);
int code = luaL_checkinteger(L, -1);
-
+
const char* code_det = http_code(code);
http_build(&resp, code, code_det, header_vs->c, content, len);
@@ -39,16 +39,16 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si
* @param {char*} response buffer
* @param {str*} response header Content-Type value
* @return {int} lua index of table
-*/
+ */
int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer, str* content_type, size_t blen, struct file_parse* _content){
struct file_parse content = *_content;
/*enum file_status* status = (enum file_status*)parray_get(content, "_status");
- str* current = (str*)parray_get(content, "_current");
- str* old = (str*)parray_get(content, "_old");
- str* boundary = (str*)parray_get(content, "_boundary");
- str* boundary_id = (str*)parray_get(content, "_boundary_id");
- int* dash_count = (int*)parray_get(content, "_dash_count");
- int* table_idx = (int*)parray_get(content, "_table_idx");*/
+ str* current = (str*)parray_get(content, "_current");
+ str* old = (str*)parray_get(content, "_old");
+ str* boundary = (str*)parray_get(content, "_boundary");
+ str* boundary_id = (str*)parray_get(content, "_boundary_id");
+ int* dash_count = (int*)parray_get(content, "_dash_count");
+ int* table_idx = (int*)parray_get(content, "_table_idx");*/
//time_start(start)
if(content.status == _ignore){
@@ -68,11 +68,11 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
content.current = str_init("");
content.table_idx = lua_gettop(L);//malloc(sizeof * table_idx); *table_idx = lua_gettop(L);
- //parray_set(content, "_table_idx", (void*)(table_idx));
- //parray_set(content, "_status", (void*)(status));
- //parray_set(content, "_dash_count", (void*)(dash_count));
- //parray_set(content, "_current", (void*)(current));
-
+ //parray_set(content, "_table_idx", (void*)(table_idx));
+ //parray_set(content, "_status", (void*)(status));
+ //parray_set(content, "_dash_count", (void*)(dash_count));
+ //parray_set(content, "_current", (void*)(current));
+
content.boundary_id = str_init("");
//quick fix?
@@ -81,7 +81,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
//parray_set(content, "_boundary", (void*)boundary);
//parray_set(content, "_boundary_id", (void*)boundary_id);
-
+
}
//time_end("start", start)
//printf("hi\n");
@@ -95,101 +95,101 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer
lua_concat(L, 2);
*body_idx = lua_gettop(L);
} else {
- file_start:;
- //time_start(barrier_read)
- if(content.status == BARRIER_READ){
- //printf("read %llu\n", blen);
- for(int i = 0; i != blen; i++){
- //printf("%c",buffer[i]);
- //printf("\n");
- if(*buffer == '\r'){
- content.status = FILE_HEADER;
- buffer += 2;
- blen -= i + 2;
-
- content.table_idx = lua_rawlen(L, *files_idx) + 1;
- lua_pushinteger(L, content.table_idx);
- lua_newtable(L);
- lua_settable(L, *files_idx);
- break;
- }
- str_pushl(content.boundary_id, buffer, 1);
- buffer++;
- }
- }
- //time_end("barrier_read", barrier_read)
- lua_pushvalue(L, *files_idx);
- lua_pushinteger(L, content.table_idx);
- lua_gettable(L, -2);
- int rfiles_idx = lua_gettop(L);
- //time_start(file_header)
- if(content.status == FILE_HEADER){
- //printf("header\n");
- for(int i = 0; i < blen; i++){
-
- if(buffer[i] == ':'){
- content.old = content.current;
- content.current = str_init("");
- } else if(buffer[i] == '\n'){
- if(content.current->len == 0){
- content.status = FILE_BODY;
-
- buffer += i + 1;
- blen -= i + 1;
-
- content.old = NULL;
- str_free(content.current);
- content.current = str_init("");
- break;
- }
- //printf("%i '%s' : '%s'\n",*table_idx, old->c, current->c);
-
- luaI_tsets(L, rfiles_idx, content.old->c, content.current->c);
-
- str_free(content.old);
- content.old = NULL;
- str_clear(content.current);
- } else if(buffer[i] != '\r' && !(buffer[i] == ' ' && content.current->len == 0)) str_pushl(content.current, buffer + i, 1);
- }
- }
- //time_end("file_header", file_header)
- //time_start(file_body)
- if(content.status == FILE_BODY){
- char* barrier_end = memmem(buffer, blen, content.boundary->c, content.boundary->len);
- if(barrier_end == NULL){
- str* temp = str_initl(content.current->c, content.current->len);
- str_pushl(temp, buffer, blen);
- barrier_end = memmem(temp->c, temp->len, content.boundary->c, content.boundary->len);
- if(barrier_end != NULL) abort(); // todo
-
- str* temp2 = content.current;
- content.current = temp;
- str_free(temp2);
-
- } else {
- char* start = barrier_end, *end = barrier_end;
- for(; *start != '\n'; start--);
- for(; *end != '\n'; end++);
- int clen = start - buffer;
- str_pushl(content.current, buffer, clen);
- luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len - 1);
- str_clear(content.current);
- blen-= end - buffer;
- buffer = end;
- content.status = BARRIER_READ;
- goto file_start;
- //printf("%s\n",content.current->c);
- }
-
- }
- //time_end("file_body", file_body)
+file_start:;
+ //time_start(barrier_read)
+ if(content.status == BARRIER_READ){
+ //printf("read %llu\n", blen);
+ for(int i = 0; i != blen; i++){
+ //printf("%c",buffer[i]);
+ //printf("\n");
+ if(*buffer == '\r'){
+ content.status = FILE_HEADER;
+ buffer += 2;
+ blen -= i + 2;
+
+ content.table_idx = lua_rawlen(L, *files_idx) + 1;
+ lua_pushinteger(L, content.table_idx);
+ lua_newtable(L);
+ lua_settable(L, *files_idx);
+ break;
+ }
+ str_pushl(content.boundary_id, buffer, 1);
+ buffer++;
+ }
+ }
+ //time_end("barrier_read", barrier_read)
+ lua_pushvalue(L, *files_idx);
+ lua_pushinteger(L, content.table_idx);
+ lua_gettable(L, -2);
+ int rfiles_idx = lua_gettop(L);
+ //time_start(file_header)
+ if(content.status == FILE_HEADER){
+ //printf("header\n");
+ for(int i = 0; i < blen; i++){
+
+ if(buffer[i] == ':'){
+ content.old = content.current;
+ content.current = str_init("");
+ } else if(buffer[i] == '\n'){
+ if(content.current->len == 0){
+ content.status = FILE_BODY;
+
+ buffer += i + 1;
+ blen -= i + 1;
+
+ content.old = NULL;
+ str_free(content.current);
+ content.current = str_init("");
+ break;
+ }
+ //printf("%i '%s' : '%s'\n",*table_idx, old->c, current->c);
+
+ luaI_tsets(L, rfiles_idx, content.old->c, content.current->c);
+
+ str_free(content.old);
+ content.old = NULL;
+ str_clear(content.current);
+ } else if(buffer[i] != '\r' && !(buffer[i] == ' ' && content.current->len == 0)) str_pushl(content.current, buffer + i, 1);
+ }
+ }
+ //time_end("file_header", file_header)
+ //time_start(file_body)
+ if(content.status == FILE_BODY){
+ char* barrier_end = memmem(buffer, blen, content.boundary->c, content.boundary->len);
+ if(barrier_end == NULL){
+ str* temp = str_initl(content.current->c, content.current->len);
+ str_pushl(temp, buffer, blen);
+ barrier_end = memmem(temp->c, temp->len, content.boundary->c, content.boundary->len);
+ if(barrier_end != NULL) abort(); // todo
+
+ str* temp2 = content.current;
+ content.current = temp;
+ str_free(temp2);
+
+ } else {
+ char* start = barrier_end, *end = barrier_end;
+ for(; *start != '\n'; start--);
+ for(; *end != '\n'; end++);
+ int clen = start - buffer;
+ str_pushl(content.current, buffer, clen);
+ luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len - 1);
+ str_clear(content.current);
+ blen-= end - buffer;
+ buffer = end;
+ content.status = BARRIER_READ;
+ goto file_start;
+ //printf("%s\n",content.current->c);
+ }
+
+ }
+ //time_end("file_body", file_body)
}
/*parray_set(content, "_dash_count", dash_count);
- parray_set(content, "_boundary_id", boundary_id);
- parray_set(content, "_boundary", boundary);
- parray_set(content, "_status", status);
- parray_set(content, "_current", current);
- parray_set(content, "_old", old);*/
+ parray_set(content, "_boundary_id", boundary_id);
+ parray_set(content, "_boundary", boundary);
+ parray_set(content, "_status", status);
+ parray_set(content, "_current", current);
+ parray_set(content, "_old", old);*/
*_content = content;
diff --git a/src/net/luai.h b/src/net/luai.h
index 5452670..7e5fb07 100644
--- a/src/net/luai.h
+++ b/src/net/luai.h
@@ -9,6 +9,6 @@ void i_write_header(lua_State* L, int header_top, str** _resp, char* content, si
* @param {char*} response buffer
* @param {str*} response header Content-Type value
* @return {int} lua index of table
-*/
+ */
int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer, str* content_type, size_t blen, struct file_parse* _content);
diff --git a/src/net/util.c b/src/net/util.c
index 61cf1b7..866e931 100644
--- a/src/net/util.c
+++ b/src/net/util.c
@@ -37,7 +37,7 @@ int64_t recv_header(int client_fd, char** _buffer, char** header_eof){
/**
* @brief calls recv into buffer until everything is read
*
-*/
+ */
// deprecated!! replaced by recv_header (above)
int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state){
char* header, *buffer = malloc(BUFFER_SIZE * sizeof * buffer);
@@ -59,13 +59,13 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
if(*header_eof == -1 && (header = strstr(buffer, "\r\n\r\n")) != NULL){
*header_eof = header - buffer;
char* cont_len_raw = strstr(buffer, "Content-Length: ");
-
+
if(cont_len_raw == NULL) {
len += n;
*_buffer = buffer;
return len;
}
-
+
str* cont_len_str = str_init("");
if(cont_len_raw == NULL) abort();
//i is length of 'Content-Length: '
@@ -92,7 +92,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
buffer = realloc(buffer, len + BUFFER_SIZE + 1);
memset(buffer + len, 0, n + 1);
}
-
+
if(content_len != -1 && len - *header_eof - 4 >= con_len_full) break;
}
@@ -104,7 +104,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st
/**
* @brief converts the request buffer into a parray_t
*
-*/
+ */
int parse_header(char* buffer, int header_eof, parray_t** _table){
if(header_eof == -1) return -1;
@@ -117,7 +117,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
if(buffer[oi] == ' ' || buffer[oi] == '\n'){
if(buffer[oi] == '\n') current->c[current->len - 1] = 0;
if(item < 3) parray_set(table, item == 0 ? "Request" :
- item == 1 ? "Path" : "Version", (void*)str_init(current->c));
+ item == 1 ? "Path" : "Version", (void*)str_init(current->c));
str_clear(current);
item++;
if(buffer[oi] == '\n') break;
@@ -174,16 +174,16 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){
/**
* @brief contructs an http request
*
-*/
+ */
void http_build(str** _dest, int code, const char* code_det, char* header_vs, char* content, size_t len){
char* dest = malloc(HTTP_BUFFER_SIZE);
memset(dest, 0, HTTP_BUFFER_SIZE);
sprintf(dest,
- "HTTP/1.1 %i %s\r\n"
- "%s"
- "\r\n"
- , code, code_det, header_vs);
+ "HTTP/1.1 %i %s\r\n"
+ "%s"
+ "\r\n"
+ , code, code_det, header_vs);
*_dest = str_init(dest);
str_pushl(*_dest, content, len);
@@ -193,7 +193,7 @@ void http_build(str** _dest, int code, const char* code_det, char* header_vs, ch
/**
* @brief gets a string representation of a http code
*
-*/
+ */
const char* http_code(int code){
switch(code){
case 100: return "Continue"; break;
@@ -384,9 +384,9 @@ parray_t* route_match(parray_t* paths, char* request, larray_t** _params){
for(int i = 0; i != paths->len; i++){
//if(match_param(paths->P[i].key->c, request))
//*if(strcmp(request, paths->P[i].key->c) == 0)*/{
- //printf("pass!\n");
+ //printf("pass!\n");
//printf("%i\n", i);
-
+
temp = parray_init();
if(match_param(paths->P[i].key->c, request, temp)){
@@ -478,7 +478,7 @@ void _parse_mimetypes(){
if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n') break;
}
type[type_len] = '\0';
-
+
//check if the type has an associated file type
if(buffer[i + type_len] == '\0' || buffer[i + type_len] == '\n'){
free(type);
diff --git a/src/net/util.h b/src/net/util.h
index b8bc824..a54ac95 100644
--- a/src/net/util.h
+++ b/src/net/util.h
@@ -11,7 +11,7 @@
* @param {char**} pointer to a unallocated buffer
* @param {int*} pointer to an int, will be where the header ends
* @return {int64_t} bytes read, -1 if the body was damaged, -2 if the header was
-*/
+ */
int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state);
int64_t recv_header(int client_fd, char** _buffer, char** header_eof);
@@ -22,7 +22,7 @@ int64_t recv_header(int client_fd, char** _buffer, char** header_eof);
* @param {int} where the header ends
* @param {parray_t**} pointer to a unallocated parray_t
* @return {int} returns 0 or -1 on failure
-*/
+ */
int parse_header(char* buffer, int header_eof, parray_t** _table);
/**
@@ -34,7 +34,7 @@ int parse_header(char* buffer, int header_eof, parray_t** _table);
* @param {char*} all other header values
* @param {char*} response content
* @param {size_t} content length
-*/
+ */
void http_build(str** _dest, int code, const char* code_det, char* header_vs, char* content, size_t len);
/**
@@ -42,7 +42,7 @@ void http_build(str** _dest, int code, const char* code_det, char* header_vs, ch
*
* @param {int} http response code
* @param {char*} allocated destination string
-*/
+ */
const char* http_code(int code);
void client_fd_errors(int client_fd);