Skip to content

Commit

Permalink
proof of concept complete - got valid http response back from ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
ricksterhd123 committed Jan 31, 2024
1 parent 6786d48 commit 49dbd05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
18 changes: 2 additions & 16 deletions config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
'<h1>Hello world!</h1>'
end
14 changes: 10 additions & 4 deletions src/http1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
46 changes: 21 additions & 25 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 49dbd05

Please sign in to comment.