Skip to content

Instantly share code, notes, and snippets.

@josephwinston
Created December 20, 2016 01:26
Show Gist options
  • Save josephwinston/fc99563cfc70967973200c81661c3d3e to your computer and use it in GitHub Desktop.
Save josephwinston/fc99563cfc70967973200c81661c3d3e to your computer and use it in GitHub Desktop.
simba http_server + websocket (fails)
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
static struct http_server_t http_server;
THRD_STACK(listener_stack, 1024);
THRD_STACK(connection_stack, 1500);
static int request_index(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p);
static int request_stream(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p);
static struct http_server_route_t routes[] = {
{ .path_p = "/index.html", .callback = request_index },
{ .path_p = "/stream", .callback = request_stream },
{ .path_p = NULL, .callback = NULL }
};
static struct http_server_listener_t listener = {
.address_p = "0.0.0.0",
.port = 8000,
.thrd = {
.name_p = "http_listener",
.stack = {
.buf_p = listener_stack,
.size = sizeof(listener_stack)
}
}
};
static struct http_server_connection_t connections[] = {
{
.thrd = {
.name_p = "http_conn_0",
.stack = {
.buf_p = connection_stack,
.size = sizeof(connection_stack)
}
}
},
{ .thrd = { .name_p = NULL } }
};
/**
* Handler for the index request.
*/
static int request_index(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p)
{
static const char index_html[] =
"<!DOCTYPE HTML>\n"
"<html>\n"
" <body>\n"
" Hello from Simba!"
" </body>\n"
"</html>\n";
struct http_server_response_t response;
/* Only the GET action is supported. */
if (request_p->action != http_server_request_action_get_t) {
return (-1);
}
/* Create the response. */
response.code = http_server_response_code_200_ok_t;
response.content.type = http_server_content_type_text_html_t;
response.content.buf_p = index_html;
response.content.size =
strlen(response.content.buf_p);
return (http_server_response_write(connection_p,
request_p,
&response));
}
static struct socket_t websocket;
static struct http_websocket_server_t websocket_server;
static uint8_t buf[256];
static int
request_stream(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p)
{
struct http_server_request_t request;
/* Initialize the request.*/
request.action = http_server_request_action_get_t;
request.headers.sec_websocket_key.present = 1;
strcpy(request.headers.sec_websocket_key.value, "");
/* Perform the handshake. */
http_websocket_server_handshake(&websocket_server,
&request);
buf[0] = 'f';
buf[1] = 'o';
buf[2] = 'o';
http_websocket_server_write(&websocket_server,
HTTP_TYPE_BINARY,
buf,
3);
return 0;
}
/**
* Default page handler.
*/
static int no_route(struct http_server_connection_t *connection_p,
struct http_server_request_t *request_p)
{
struct http_server_response_t response;
/* Create the response. */
response.code = http_server_response_code_404_not_found_t;
response.content.type = http_server_content_type_text_html_t;
response.content.buf_p = NULL;
response.content.size = 0;
return (http_server_response_write(connection_p,
request_p,
&response));
}
#if defined(ARCH_ESP) || defined(ARCH_ESP32)
static int init()
{
sys_start();
http_server_init(&http_server,
&listener,
connections,
NULL,
routes,
no_route);
http_websocket_server_init(&websocket_server,
&websocket);
http_server_start(&http_server);
return (0);
}
#else
static struct uart_driver_t ipuart;
static char iprxbuf[512];
static struct network_interface_slip_t slip;
static THRD_STACK(stack, 1024);
static void *slip_reader(void *arg_p)
{
uint8_t data;
while (1) {
uart_read(&ipuart, &data, 1);
network_interface_slip_input(&slip, data);
}
return (NULL);
}
static int init()
{
struct inet_ip_addr_t ipaddr;
struct inet_ip_addr_t netmask;
struct inet_ip_addr_t gw;
sys_start();
std_printf(sys_get_info());
uart_init(&ipuart, &uart_device[1], 115200, iprxbuf, sizeof(iprxbuf));
uart_start(&ipuart);
inet_module_init();
socket_module_init();
network_interface_slip_module_init();
inet_aton("169.254.1.2", &ipaddr);
inet_aton("255.255.255.0", &netmask);
inet_aton("0.0.0.0", &gw);
network_interface_slip_init(&slip,
&ipaddr,
&netmask,
&gw,
&ipuart.chout);
network_interface_add(&slip.network_interface);
network_interface_start(&slip.network_interface);
thrd_spawn(slip_reader,
NULL,
0,
stack,
sizeof(stack));
http_server_init(&http_server,
&listener,
connections,
NULL,
routes,
no_route);
http_websocket_server_init(&websocket_server,
&websocket);
http_server_start(&http_server);
return (0);
}
#endif
int main()
{
init();
thrd_suspend(NULL);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment