# yhs **Repository Path**: studvc/yhs ## Basic Information - **Project Name**: yhs - **Description**: Embeddable HTTP server. - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-01-05 - **Last Updated**: 2021-01-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #+OPTIONS: toc:nil num:nil author:nil email:nil creator:nil timestamp:nil ^:nil #+TITLE: yocto HTTP server The yocto HTTP server is a small embeddable web server, with a convenient public domain licence. Use it to add a web server to your program for debugging, introspection or remote control. You specify the paths of "files" and "folders" you want to make available, and callbacks to be called when they are requested, and the yocto HTTP server handles the rest. When your callback is called, you can use convenient stdio-style functions to send text or binary data to the browser, or transmit image data. Of course, if you just want some files serving from folders on disk, the yocto HTTP server will do that. Also, WebSockets. The yocto HTTP server has been written for ease of embedding and ease of use, under the assumption that it will be used as a development and debugging aid. Security and performance were not design goals. * Installation iOS, Mac OS X and Windows (VC++) are supported. It can be built as C89, C99 or C++. 1. Add =yhs.c= and =yhs.h= to your project; 2. Include =yhs.h= in files that need it; 3. Add function calls as described below; 4. Add =#ifdef= (etc.) to make very sure you won't ship with it running; 5. PROFIT. * Use This file provides a conversational overview. Please consult the header file as well. ** Start, update and stop server A particular server (serving a particular tree of "files" on a particular port) is represented by a =yhsServer= pointer: : yhsServer *server; Create one using =yhs_new_server=, supplying port: : server = yhs_new_server(80); You can name your server, if you like. Its name will appear in any error pages. : yhs_set_server_name(server,"my amazing server"); Each time round your main loop, call =yhs_update= to keep the server ticking over: : yhs_update(server); When you're done, call =yhs_delete_server= to free up the server and its resources: : yhs_delete_server(server); : server=NULL; ** Adding things to serve Use =yhs_add_res_path_handler= to add a callback (see below) for a particular path: : yhs_add_res_path_handler(server,"/res/",&handle_root,NULL); The argument for =context= is stored and made available to the callback. Paths not ending in =/= are considered files, and their callback will be called when a request is made for that exact path. Paths ending in =/= are considered folders, meaning the callback will be called for any file in that folder (at whatever depth), if there isn't a closer-matching folder or file handler for it. If there's no handler added for the root folder =/=, =GET= requests for =/= will be responded to automatically with a contents page. The server will respond to any other unhandled path with a 404 page. ** Serving things The handler callback has the following signature: : extern "C" typedef void (*yhsResPathHandlerFn)(yhsRequest *re); =re= points to the (opaque) request object. There are various functions to get details about the request: - =yhs_get_path= retrieves the specified path, and =yhs_get_path_handler_relative= retrieves the part that's relative to the path supplied to =yhs_add_res_path_handler=. - =yhs_get_method= and =yhs_get_method_str= retrieve the HTTP method. =yhs_get_method= returns one of the values from the (not exhaustive) =yhsMethod= enum, and =yhs_get_method_str= returns the actual method name string. - =yhs_find_header_field= allows the request header fields to be queried. You can also use =yhs_get_handler_context= and =yhs_get_handler_path= to retrieve the values supplied to =yhs_add_res_path_handler=. On entry to the callback, any content is available for reading (if you want it), and the server is ready for your callback to provide a response, as described below. Once you have sent the response, just return from the callback and appropriate action will be taken automatically. If your callback doesn't provide any response, the server will automatically provide a 404 page. (You can respond to a =HEAD= request in exactly the same way as a =GET= request. The server checks for =HEAD= specially, and will discard any response body in that case, leaving just the headers.) *** Data response Use =yhs_begin_data_response= to start a data response, supplying MIME type of data being sent: : yhs_begin_data_response(re,"text/html"); Then use =yhs_text= (works like =printf=) to send raw text: : yhs_text(re,"
%d
",rand()); Also available are =yhs_textv= (works like =vprintf=), =yhs_text= (works like =fputs=), =yhs_data= (works a bit like =fwrite=), and =yhs_data_byte= (works a bit like =fputc=). If you're responding with HTML, there are a set of convenience functions, =yhs_html_text*=, which can add in HTML escapes and optionally replace =\n= with =