|
pianod2
multisource multiuser scriptable networked music player
|
Football HTTP/Websocket functions. More...
#include <config.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#include <stdbool.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <netinet/in.h>#include <assert.h>#include <ctype.h>#include <sys/stat.h>#include <time.h>#include "fb_public.h"#include "fb_service.h"#include "sha1.h"
Classes | |
| struct | media_dictionary_t |
| struct | language_rank |
Macros | |
| #define | _POSIX_C_SOURCE 200112L /* fileno() */ |
| #define | _BSD_SOURCE /* strdup(), realpath() */ |
| #define | _DARWIN_C_SOURCE /* strdup() on OS X */ |
| #define | _DEFAULT_SOURCE |
| #define | countof(x) (sizeof (x) / sizeof (*x)) |
| #define | WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| #define | WEBSOCKET_VERSION "13" |
| #define | HTTP_VERSION "HTTP/1.1" |
| #define | get_hex_digit(c) |
Typedefs | |
| typedef enum ws_opcodes_t | OPCODE |
| typedef struct media_dictionary_t | MEDIA_DICTIONARY |
| typedef struct language_rank | LANGUAGE_RANK |
Enumerations | |
| enum | ws_opcodes_t { WSOC_CONTINUATION = 0x00 , WSOC_TEXT = 0x01 , WSOC_BINARY = 0x02 , WSOC_CLOSE = 0x08 , WSOC_PING = 0x09 , WSOC_PONG = 0x0a , WSOC_MASK = 0x0f } |
Functions | |
| static bool | fb_queue_http_noflush (FB_CONNECTION *connection, char *message, size_t length) |
| static bool | fb_queue_http (FB_CONNECTION *connection, char *message, size_t length) |
| static bool | fb_queue_http_alloc (FB_CONNECTION *connection, const char *message, size_t length) |
| static bool | write_message (FB_CONNECTION *connection, const char *message) |
| static bool | http_header (FB_CONNECTION *connection, const char *message, const char *extra_header, const char *detail) |
| static bool | http_response (FB_CONNECTION *connection, const char *message) |
| static bool | http_redirect (FB_CONNECTION *connection, const char *location) |
| static void | encodeblock (unsigned char *in, unsigned char *out, int len) |
| static void | perform_unmask (unsigned char *message, const unsigned long length, const unsigned char *mask) |
| static bool | fb_get_http_bytes (FB_CONNECTION *connection, size_t message_size) |
| void | reset_ping_interval (FB_CONNECTION *connection) |
| Reset the next-ping interval. More... | |
| FB_EVENT * | fb_read_websocket_input (FB_EVENT *event, FB_CONNECTION *connection) |
| bool | fb_websocket_ping (FB_CONNECTION *connection) |
| bool | fb_websocket_announce_close (FB_CONNECTION *connection) |
| bool | fb_websocket_encode (FB_CONNECTION *connection) |
| static bool | fb_greet_websocket (FB_CONNECTION *connection) |
| void | fb_destroy_httprequest (FB_HTTPREQUEST *request) |
| static bool | redirect_to_subdirectory (FB_CONNECTION *connection) |
| static bool | url_decode (char *request) |
| void | fb_collect_http_request (FB_EVENT *event, FB_HTTPREQUEST *request) |
| static bool | store (char **value, const char *newvalue, FB_HTTPREQUEST *request) |
| void | fb_collect_http_parameter (char *line, FB_HTTPREQUEST *request) |
| bool | fb_http_command (const char *command) |
| static const char * | get_media_type (const char *filename) |
| static bool | http_serve_data (FB_CONNECTION *connection, char *name, FILE *file, bool sendbody) |
| static bool | malicious_request (FB_HTTPREQUEST *request, const char *filename) |
| static bool | request_malfunction (FB_CONNECTION *connection, const char *file_path) |
| static char * | next_language (char *languages) |
| static int | fb_language_compare (const void *elem1, const void *elem2) |
| static LANGUAGE_RANK * | parse_language_list (int socket, char *languages, int *count) |
| static FILE * | http_file_open (FB_CONNECTION *connection, const char *file_name, LANGUAGE_RANK *languages, int language_count, char **file_path) |
| static bool | http_file_request (FB_CONNECTION *connection) |
| FB_EVENT * | fb_execute_http_request (FB_EVENT *event, FB_CONNECTION *connection) |
Variables | |
| static const unsigned int | WS_OPCODE = 0 |
| static const unsigned int | WS_PAYLOAD = 1 |
| static const unsigned int | WS_HEADER_MAXIMUM = 32 |
| static const unsigned char | WS_FIN = 0x80 |
| static const unsigned char | WS_MASK = 0x80 |
| static const unsigned char | WS_PAYLOAD_MASK = 0x7f |
| static const unsigned int | WS_PAYLOAD_MAX_8BIT = 125 |
| static const unsigned int | WS_PAYLOAD_MAGIC_16BIT = 126 |
| static const unsigned int | WS_PAYLOAD_MAGIC_64BIT = 127 |
| static const unsigned int | WS_PAYLOAD_MAX_16BIT = 0xffff |
| static const char | cb64 [] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
Football HTTP/Websocket functions.
| #define _BSD_SOURCE /* strdup(), realpath() */ |
| #define _DARWIN_C_SOURCE /* strdup() on OS X */ |
| #define _DEFAULT_SOURCE |
| #define _POSIX_C_SOURCE 200112L /* fileno() */ |
| #define countof | ( | x | ) | (sizeof (x) / sizeof (*x)) |
| #define get_hex_digit | ( | c | ) |
| #define HTTP_VERSION "HTTP/1.1" |
| #define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| #define WEBSOCKET_VERSION "13" |
| typedef struct language_rank LANGUAGE_RANK |
Private structure for language rankings
| typedef struct media_dictionary_t MEDIA_DICTIONARY |
Map file extensions to MIME types reported by HTTP server.
| typedef enum ws_opcodes_t OPCODE |
| enum ws_opcodes_t |
|
static |

| void fb_collect_http_parameter | ( | char * | line, |
| FB_HTTPREQUEST * | request | ||
| ) |
Collect HTTP method parameters that are relevant.
| line | the HTTP header line received. |
| request | the request structure into which details will be stored. |


| void fb_collect_http_request | ( | FB_EVENT * | event, |
| FB_HTTPREQUEST * | request | ||
| ) |
Collect the relevant bits of 'get', 'head' or other HTTP method.
| event | an event, complete with argv structure. |
| request | HTTP request structure into which details will be placed. |


| void fb_destroy_httprequest | ( | FB_HTTPREQUEST * | request | ) |
Reset a request structure. Frees dynamically allocated elements and clears structure.
| request | The request structure to reset. |

| FB_EVENT* fb_execute_http_request | ( | FB_EVENT * | event, |
| FB_CONNECTION * | connection | ||
| ) |
Interpret the collected HTTP request. Return an HTTP error if the request is invalid. If it is a WebSocket request, initiate a WebSocket session. Otherwise serve the request and reset the connection.
| event | a partially filled event applicable to this connection. |
| connection | the connection being served. |


|
static |
Ensure that input buffer contains required number of bytes. Read more input if necessary.
| connection | the connection read from. |
| message_size | The amount of data we need to collect. |


|
static |
Respond to the HTTP WebSocket request with the connection upgrade response. This requires crafting a custom SHA1 key to confirm we're really speaking WebSocket.
| connection | The connection to greet. |


| bool fb_http_command | ( | const char * | command | ) |
Determine if a request is an HTTP one.
| command | the HTTP request (GET, HEAD, etc.) |


|
static |
A qsort() etc compliant callback that compares languages for sorting.
| elem1 | the first language rank for comparison. |
| elem2 | the other language rank for comparison. |

|
inlinestatic |
Add a message to the output queue. The message must be dynamically allocated by caller and is added to the connections queue. On failure. it is freed by this function.
| connection | the destination of the message |
| message | the message body, dynamically allocated by the caller. |
| length | the length of message. |


|
static |
Add a message to the output queue, dynamically allocating the message.
| connection | Where to send the message. |
| message | The message to send. |
| length | The length of message. |


|
static |
Same as fb_queue_http, but does not flush output after queueing.


| FB_EVENT* fb_read_websocket_input | ( | FB_EVENT * | event, |
| FB_CONNECTION * | connection | ||
| ) |
Read data from WebSocket connection. Reads a WebSocket packet and decodes it, and returns an event for the payload.
| event | a partially filled event applicable to the connection. |
| connection | the connection to read from. |


| bool fb_websocket_announce_close | ( | FB_CONNECTION * | connection | ) |
Send a WebSocket close packet. This should be called as last thing prior to closing the connection.
| connection | The connection to write the close to. |

| bool fb_websocket_encode | ( | FB_CONNECTION * | connection | ) |
Build WebSocket packets from output. Checks if there's a complete packet (line) in the assembly queue. When there is, assemble a WebSocket packet and put it in the output queue.
| connection | The connection to check. |


| bool fb_websocket_ping | ( | FB_CONNECTION * | connection | ) |
Send a WebSocket ping packet. Some browsers have timeouts, so we need to send pings as keep-alives.
| connection | The connection to check. |


|
static |
Guess the media type from a file's extension.
| filename | the name of a file. |


|
static |
Handle opening a file, accounting for preferred language.
| connection | The connection and request for which file is being opened. |
| file_name | The name of the file to open, before language mangling. |
| languages | The list of languages from Accept-Language header. |
| language_count | The number of languages in the list. |
| file_path | The name of the file that was opened. It is the caller's responsibility to free this, regardless of whether http_file_open succeeded. |

|
static |
Handle a GET or HEAD request. Generate appropriate HTTP error messages if the file is inaccessible or does not exist.


|
static |
Send an HTTP response header to a client. Closes the connection if anything goes wrong.
| connection | Where to send the header. |
| message | The HTTP response code/text. |
| extra_header | Extra line(s) to include with the header, or NULL. |
| detail | Text to provide to the user, or NULL to go with the default. |


|
static |
Create a properly formatted HTTP redirect response.
| connection | Where to send the response. |
| location | The URL to redirect to. |


|
static |
Create a properly formatted HTTP response header.
| connection | Where to send the response. |
| message | The HTTP response code/text. |


|
static |
Handle a complete and valid GET or HEAD request.
| connection | the connection issuing the request. |
| name | the name of the file being served. |
| file | an open file handle for the file being served. |
| sendbody | true if the file should be served (GET request), false if not (HEAD) |


|
static |
Determine request maliciousness. Requests for a language with a '.' or '/' characters are malicious. Assess the filename of a GET/HEAD request to see if it looks maliciously crafted. Currently, disallow hidden files or parent directories.
| request | the request details |
| filename | the requested pathname. |

|
static |

|
static |
Parse an Accept-Language header into a list of languages.
| socket | The connection's socket, to which error messages are sent. |
| languages | The Accept-Language header value. Modified by this function. |
| count | Set to the number of languages found on return. |


|
static |
Mask or umask a websocket packet.
| message | the message to be masked/unmasked. |
| length | the length of the message. |
| mask | Pointer to a 4 bytes containing mask . |

|
static |
Redirect to a subdirectory. Given a request pointing to a directory (without trailing slash), request the contents (with slash).
| connection | The client to redirect. |


|
static |
Determine if request path is valid on the server side.
| connection | the connection issuing the request |
| file_path | the requested pathname. |

| void reset_ping_interval | ( | FB_CONNECTION * | connection | ) |
Reset the next-ping interval.

|
static |
Store a value in the request structure, or consider it an error if it's duplicate.
| value | The destination of the value. |
| newvalue | The value store. |
| request | The request structure. |

|
static |
Decode URL encoding.
| request | the URL to decode. Contents are overwritten with decoded URL. |

|
static |
Send some raw text to a connection.
| connection | Where to send the message. |
| message | The message to send. |


|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |