diff --git a/config.rb b/config.rb
index 6356e87..82bcd43 100755
--- a/config.rb
+++ b/config.rb
@@ -3,20 +3,6 @@
##
# mruv socket handler function fizzbuzz example
#
-def handler(event, context)
- puts event, context
- n = event[:body].chomp.to_i
- if n.positive? && (n < 100)
- if (n % 15).zero?
- 'fizzbuzz'
- elsif (n % 3).zero?
- 'fizz'
- elsif (n % 5).zero?
- 'buzz'
- else
- env
- end
- else
- 'Must be number between 1 and 99'
- end
+def handler
+ '
Hello world!
'
end
diff --git a/src/http1.h b/src/http1.h
index 1ad137a..20cf546 100644
--- a/src/http1.h
+++ b/src/http1.h
@@ -151,6 +151,10 @@ http_response_t *generate_http_response(unsigned int status_code, const char *bo
response->http_date = new_str(asctime(gmtime(&result)));
response->http_date_len = strlen(response->http_date) + 1;
+ // Remove the \n from the end of the string
+ // \n\0
+ response->http_date[response->http_date_len - 2] = '\0';
+
response->status_code = status_code;
response->status_reason = get_status_reason_code(status_code);
response->status_reason_len = strlen(response->status_reason) + 1;
@@ -170,7 +174,8 @@ http_response_t *generate_http_response(unsigned int status_code, const char *bo
http_header_t *last_modified = get_http_header("Last-Modified", response->http_date);
http_header_t *accept_ranges = get_http_header("Accept-Ranges", "none");
http_header_t *vary = get_http_header("Vary", "Accept-Encoding");
- http_header_t *content_type = get_http_header("Content-Type", "text/plain");
+ http_header_t *connection = get_http_header("Connection", "Closed");
+ http_header_t *content_type = get_http_header("Content-Type", "text/html");
response->headers = (http_header_t **)malloc(sizeof(http_header_t *) * 100);
response->headers[0] = date;
@@ -179,8 +184,9 @@ http_response_t *generate_http_response(unsigned int status_code, const char *bo
response->headers[3] = accept_ranges;
response->headers[4] = content_length;
response->headers[5] = vary;
- response->headers[6] = content_type;
- response->num_headers = 7;
+ response->headers[6] = connection;
+ response->headers[7] = content_type;
+ response->num_headers = 8;
free(content_length_str);
@@ -239,7 +245,7 @@ char *http_response_to_str(http_response_t *response)
size_t total_response_len = status_line_len + 2 + headers_str_len + 2 + response->body_len;
char* response_str = (char*) calloc(total_response_len, sizeof(char));
- snprintf(response_str, total_response_len, "%s\r\n%s", headers_str, response->body);
+ snprintf(response_str, total_response_len, "%s\r\n%s\r\n%s", status_line, headers_str, response->body);
free(headers_str);
free(status_line);
diff --git a/src/main.c b/src/main.c
index 5bd6f20..537d819 100755
--- a/src/main.c
+++ b/src/main.c
@@ -89,31 +89,34 @@ void on_read_chunk(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
printf("%jd INFO: IP Address %s, nread %ld\n", (intmax_t)time(NULL), ip, nread);
write_req_t *req = (write_req_t *)malloc(sizeof(write_req_t));
- size_t num_headers = 100;
- struct phr_header headers[100];
- const char *method;
- int minor_version;
- size_t method_len;
- const char *path;
- size_t path_len;
-
- int pret = phr_parse_request(buf->base, nread, &method, &method_len, &path, &path_len, &minor_version, headers, &num_headers, 0);
-
- if (pret > 0)
+ http_request_t *http_request = parse_http_request(buf->base, nread);
+ if (http_request == NULL)
{
- printf("%d, %s, %s", minor_version, path, method);
+ fprintf(stderr, "Failed to parse http message\n");
+ return;
}
- else if (pret == -1)
+
+ mrb_value ret = mrb_funcall(mrb, config_script, "handler", 0);
+
+ mrb_value ret_as_str = mrb_obj_as_string(mrb, ret);
+ const char *ret_as_cstr_tmp = mrb_string_value_cstr(mrb, &ret_as_str);
+
+ http_response_t *http_response = generate_http_response(200, ret_as_cstr_tmp, strlen(ret_as_cstr_tmp) + 1);
+
+ if (http_response == NULL)
{
- fprintf(stderr, "HTTP/1.1 parse error\n");
- uv_close((uv_handle_t *)client, NULL);
+ fprintf(stderr, "Failed to generate http response\n");
+ return;
}
- char *ret = malloc(sizeof(char) * 6);
- sprintf(ret, "Hello");
+ char *http_response_str = http_response_to_str(http_response);
+ printf("%s\n", http_response_str);
- req->buf = uv_buf_init(ret, strlen(ret) + 1);
+ req->buf = uv_buf_init(http_response_str, strlen(http_response_str) + 1);
uv_write((uv_write_t *)req, client, &req->buf, 1, on_write_complete);
+ free_http_response(http_response);
+ uv_close((uv_handle_t *)client, NULL);
+ free(buf->base);
}
}
@@ -142,13 +145,6 @@ void on_new_connection(uv_stream_t *server, int status)
int main()
{
int error = 0;
-
- const char body[] = "Hello";
- http_response_t* http_response = generate_http_response(200, body, strlen(body) + 1);
- char* response_str = http_response_to_str(http_response);
-
- printf("%s\n", response_str);
-
// Load config.rb file
FILE *fp = fopen("config.rb", "r");