Skip to content

Instantly share code, notes, and snippets.

@aka-z
Forked from martinsik/libwebsockets-webserver.c
Created December 18, 2019 03:53
Show Gist options
  • Save aka-z/3639b83d4494e329ecbfe394aec76aa9 to your computer and use it in GitHub Desktop.
Save aka-z/3639b83d4494e329ecbfe394aec76aa9 to your computer and use it in GitHub Desktop.

Revisions

  1. @martinsik martinsik revised this gist Dec 5, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion libwebsockets-webserver.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    @@ -27,6 +26,7 @@ static int callback_http(struct libwebsocket_context *context,
    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597
    libwebsocket_write(wsi, universal_response,
    strlen(universal_response), LWS_WRITE_HTTP);
    break;

    } else {
    // try to get current working directory
  2. @martinsik martinsik revised this gist Aug 5, 2012. 1 changed file with 17 additions and 9 deletions.
    26 changes: 17 additions & 9 deletions libwebsockets-webserver.c
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,8 @@ static int callback_http(struct libwebsocket_context *context,
    if (strcmp(requested_uri, "/") == 0) {
    void *universal_response = "Hello, World!";
    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597
    libwebsocket_write(wsi, universal_response, strlen(universal_response), LWS_WRITE_HTTP);
    libwebsocket_write(wsi, universal_response,
    strlen(universal_response), LWS_WRITE_HTTP);

    } else {
    // try to get current working directory
    @@ -35,14 +36,18 @@ static int callback_http(struct libwebsocket_context *context,
    if (getcwd(cwd, sizeof(cwd)) != NULL) {
    // allocate enough memory for the resource path
    resource_path = malloc(strlen(cwd) + strlen(requested_uri));

    // join current working direcotry to the resource path
    sprintf(resource_path, "%s%s", cwd, requested_uri);
    printf("resource path: %s\n", resource_path);

    char *extension = strrchr(resource_path, '.');
    char *mime;

    // choose mime type based on the file extension
    char *mime;
    if (strcmp(extension, ".png") == 0) {
    if (extension == NULL) {
    mime = "text/plain";
    } else if (strcmp(extension, ".png") == 0) {
    mime = "image/png";
    } else if (strcmp(extension, ".jpg") == 0) {
    mime = "image/jpg";
    @@ -66,7 +71,8 @@ static int callback_http(struct libwebsocket_context *context,
    }

    // close connection
    libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NORMAL);
    libwebsocket_close_and_free_session(context, wsi,
    LWS_CLOSE_STATUS_NORMAL);
    break;
    }
    default:
    @@ -95,7 +101,7 @@ static struct libwebsocket_protocols protocols[] = {

    int main(void) {
    // server url will be http://localhost:7681
    int port = 7681;
    int port = 8080;
    const char *interface = NULL;
    struct libwebsocket_context *context;
    // we're not using ssl
    @@ -116,13 +122,15 @@ int main(void) {

    printf("starting server...\n");

    // infinite loop, the only option to end this serer is by sending SIGTERM. (CTRL+C)
    // infinite loop, the only option to end this serer is
    // by sending SIGTERM. (CTRL+C)
    while (1) {
    libwebsocket_service(context, 50);
    // libwebsocket_service will process all waiting events with their callback
    // functions and then wait 50 ms.
    // libwebsocket_service will process all waiting events with their
    // callback functions and then wait 50 ms.
    // (this is single threaded webserver and this will keep
    // our server from generating load while there are not requests to process)
    // our server from generating load while there are not
    // requests to process)
    }

    libwebsocket_context_destroy(context);
  3. @martinsik martinsik revised this gist Jul 31, 2012. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions libwebsockets-webserver.c
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ static int callback_http(struct libwebsocket_context *context,
    break;
    }
    default:
    printf("Unhandled callback\n");
    printf("unhandled callback\n");
    break;
    }

    @@ -94,7 +94,6 @@ static struct libwebsocket_protocols protocols[] = {
    };

    int main(void) {

    // server url will be http://localhost:7681
    int port = 7681;
    const char *interface = NULL;
    @@ -105,7 +104,7 @@ int main(void) {
    // no special options
    int opts = 0;


    // create libwebsocket context representing this server
    context = libwebsocket_create_context(port, interface, protocols,
    libwebsocket_internal_extensions,
    cert_path, key_path, -1, -1, opts);
    @@ -115,12 +114,15 @@ int main(void) {
    return -1;
    }

    printf("Starting...\n");


    printf("starting server...\n");

    // infinite loop, the only option to end this serer is by sending SIGTERM. (CTRL+C)
    while (1) {
    libwebsocket_service(context, 50);
    // libwebsocket_service will process all waiting events with their callback
    // functions and then wait 50 ms.
    // (this is single threaded webserver and this will keep
    // our server from generating load while there are not requests to process)
    }

    libwebsocket_context_destroy(context);
  4. @martinsik martinsik created this gist Jul 31, 2012.
    129 changes: 129 additions & 0 deletions libwebsockets-webserver.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <libwebsockets.h>


    static int callback_http(struct libwebsocket_context *context,
    struct libwebsocket *wsi,
    enum libwebsocket_callback_reasons reason, void *user,
    void *in, size_t len)
    {

    switch (reason) {
    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n260
    case LWS_CALLBACK_CLIENT_WRITEABLE:
    printf("connection established\n");

    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n281
    case LWS_CALLBACK_HTTP: {
    char *requested_uri = (char *) in;
    printf("requested URI: %s\n", requested_uri);

    if (strcmp(requested_uri, "/") == 0) {
    void *universal_response = "Hello, World!";
    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/libwebsockets.h#n597
    libwebsocket_write(wsi, universal_response, strlen(universal_response), LWS_WRITE_HTTP);

    } else {
    // try to get current working directory
    char cwd[1024];
    char *resource_path;

    if (getcwd(cwd, sizeof(cwd)) != NULL) {
    // allocate enough memory for the resource path
    resource_path = malloc(strlen(cwd) + strlen(requested_uri));
    // join current working direcotry to the resource path
    sprintf(resource_path, "%s%s", cwd, requested_uri);

    char *extension = strrchr(resource_path, '.');

    // choose mime type based on the file extension
    char *mime;
    if (strcmp(extension, ".png") == 0) {
    mime = "image/png";
    } else if (strcmp(extension, ".jpg") == 0) {
    mime = "image/jpg";
    } else if (strcmp(extension, ".gif") == 0) {
    mime = "image/gif";
    } else if (strcmp(extension, ".html") == 0) {
    mime = "text/html";
    } else if (strcmp(extension, ".css") == 0) {
    mime = "text/css";
    } else {
    mime = "text/plain";
    }

    // by default non existing resources return code 400
    // for more information how this function handles headers
    // see it's source code
    // http://git.warmcat.com/cgi-bin/cgit/libwebsockets/tree/lib/parsers.c#n1896
    libwebsockets_serve_http_file(wsi, resource_path, mime);

    }
    }

    // close connection
    libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NORMAL);
    break;
    }
    default:
    printf("Unhandled callback\n");
    break;
    }

    return 0;
    }

    /* list of supported protocols and callbacks */

    static struct libwebsocket_protocols protocols[] = {
    /* first protocol must always be HTTP handler */

    {
    "http-only", /* name */
    callback_http, /* callback */
    0 /* per_session_data_size */
    },
    {
    NULL, NULL, 0 /* End of list */
    }

    };

    int main(void) {

    // server url will be http://localhost:7681
    int port = 7681;
    const char *interface = NULL;
    struct libwebsocket_context *context;
    // we're not using ssl
    const char *cert_path = NULL;
    const char *key_path = NULL;
    // no special options
    int opts = 0;


    context = libwebsocket_create_context(port, interface, protocols,
    libwebsocket_internal_extensions,
    cert_path, key_path, -1, -1, opts);

    if (context == NULL) {
    fprintf(stderr, "libwebsocket init failed\n");
    return -1;
    }

    printf("Starting...\n");



    while (1) {
    libwebsocket_service(context, 50);
    }

    libwebsocket_context_destroy(context);

    return 0;
    }