Skip to content

Latest commit

 

History

History
99 lines (72 loc) · 2.6 KB

HTTP.md

File metadata and controls

99 lines (72 loc) · 2.6 KB

Example: HTTP Server

A simple HTTP server with a single endpoint in two easy steps:

  1. Define an HTTP Callback
  2. Create and Start the Server

Fin!

Let's have a peek:

1. Define an HTTP callback

User-space HTTP callback invoked by ymo_http when a new request is received from an HTTP session.

  • session: the session is an HTTP protocol wrapper around the connection
  • request: the yimmo object containing information on the request
  • response: the response object to be filled out
  • user_data: user data associated with this session (NULL here; see the API docs for more info)

Return YMO_OKAY on success; a value from errno.h on error.

NOTE: Yimmo sends all payload data (i.e. message bodies) using Apache-esque "bucket brigades" (ymo_bucket_t).

For more info, see:

static ymo_status_t test_http_callback(
        ymo_http_session_t* session,
        ymo_http_request_t* request,
        ymo_http_response_t* response,
        void* user_data)
{
    ymo_log_info("HTTP: >%s %s<", request->method, request->uri);

    if( request->content_length ) {
        ymo_log_info("Got body data: %s", request->body);
    }

    ymo_http_response_insert_header(response, "content-type", "text/html");
    ymo_http_response_set_status_str(response, "200 OK");
    ymo_bucket_t* content = YMO_BUCKET_FROM_REF("OK", 2);

    ymo_http_response_body_append(response, content);
    ymo_http_response_finish(response);
    return YMO_OKAY;
}

2. Create and Start the Server!

int main(int argc, char** argv)
{
    ymo_log_init();
    ymo_server_t* http_srv = NULL;

    /* Say hello */
    issue_startup_msg();

    ymo_http_upgrade_handler_t* handlers[2] = {
        ymo_http2_no_upgrade_handler(),
        NULL,
    };

    /* libev loop init: */
    struct ev_loop* loop = ev_default_loop(0);

    /* Create the HTTP server: */
    http_srv = ymo_http_simple_init(
            loop, HTTP_PORT, &test_http_callback, handlers, NULL);

    if( !http_srv ) {
        ymo_log_error("Unable to create server: %s", strerror(errno));
        return -1;
    }

    /* Install a sigint handler so we can terminate gracefully: */
    ev_signal sigint_watcher;
    ev_signal_init(&sigint_watcher, sigint_handler, SIGINT);
    ev_signal_start(loop, &sigint_watcher);

    /* Start it up! */
    ymo_server_start(http_srv, loop);
    ev_run(loop,0);
    ymo_log(YMO_LOG_INFO, "Shutting down!");
    ymo_server_free(http_srv);
    return 0;
}