Created
May 22, 2013 13:45
-
-
Save jorgebay/5627637 to your computer and use it in GitHub Desktop.
Haywire request without using a response struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "haywire.h" | |
| #include "http_request.h" | |
| #include "http_parser.h" | |
| #include "http_server.h" | |
| #include "http_request_context.h" | |
| #include "trie/radix.h" | |
| #include "trie/route_compare_method.h" | |
| #define CRLF "\r\n" | |
| static const char response_404[] = | |
| "HTTP/1.1 404 Not Found" CRLF | |
| "Server: Haywire/master" CRLF | |
| "Date: Fri, 26 Aug 2011 00:31:53 GMT" CRLF | |
| "Connection: Keep-Alive" CRLF | |
| "Content-Type: text/html" CRLF | |
| "Content-Length: 16" CRLF | |
| CRLF | |
| "404 Not Found" CRLF | |
| ; | |
| char * hw_get_request_header(http_request *request, char *key) | |
| { | |
| return (char *)rxt_get(key, (rxt_node *)request->headers); | |
| } | |
| int http_request_on_message_begin(http_parser* parser) | |
| { | |
| http_request_context *context = (http_request_context *)parser->data; | |
| if (context->request != NULL) | |
| { | |
| free(context->request->url); | |
| free(context->request->body); | |
| rxt_free((rxt_node *)context->request->headers); | |
| free(context->request); | |
| } | |
| context->request = malloc(sizeof(http_request)); | |
| context->request->url = NULL; | |
| context->request->headers = rxt_init(); | |
| context->request->body = NULL; | |
| return 0; | |
| } | |
| int http_request_on_url(http_parser *parser, const char *at, size_t length) | |
| { | |
| http_request_context *context = (http_request_context *)parser->data; | |
| char *data = (char *)malloc(sizeof(char) * length + 1); | |
| strncpy(data, at, length); | |
| data[length] = '\0'; | |
| context->request->url = data; | |
| return 0; | |
| } | |
| int http_request_on_header_field(http_parser *parser, const char *at, size_t length) | |
| { | |
| http_request_context *context = (http_request_context *)parser->data; | |
| char *data = (char *)malloc(sizeof(char) * length + 1); | |
| strncpy(data, at, length); | |
| data[length] = '\0'; | |
| rxt_put("$CURRENT_HEADER", data, (rxt_node *)context->request->headers); | |
| return 0; | |
| } | |
| int http_request_on_header_value(http_parser *parser, const char *at, size_t length) | |
| { | |
| http_request_context *context = (http_request_context *)parser->data; | |
| char *header = (char *)rxt_get("$CURRENT_HEADER", (rxt_node *)context->request->headers); | |
| char *data = (char *)malloc(sizeof(char) * length + 1); | |
| strncpy(data, at, length); | |
| data[length] = '\0'; | |
| rxt_put(header, data, (rxt_node *)context->request->headers); | |
| return 0; | |
| } | |
| int http_request_on_headers_complete(http_parser* parser) | |
| { | |
| return 0; | |
| } | |
| int http_request_on_body(http_parser *parser, const char *at, size_t length) | |
| { | |
| return 0; | |
| } | |
| int bytes_added(int result_of_sprintf) | |
| { | |
| return (result_of_sprintf > 0) ? result_of_sprintf : 0; | |
| } | |
| int http_request_on_message_complete(http_parser* parser) | |
| { | |
| int length = 0; | |
| uv_write_t *write_req = (uv_write_t *)malloc(sizeof(*write_req)); | |
| http_request_context *context = (http_request_context *)parser->data; | |
| uv_buf_t resbuf; | |
| resbuf.base = "HTTP/1.1 200 OK" CRLF "Content-Type: text/html" CRLF "Content-Length: 5" CRLF CRLF "Hello"; | |
| resbuf.len = strlen(resbuf.base); | |
| uv_write(write_req, (uv_stream_t*)&context->stream, &resbuf, 1, dummy_after_write); | |
| return 0; | |
| } | |
| void dummy_after_write(uv_write_t* write_req, int status) | |
| { | |
| free(write_req); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment