Prusa MINI Firmware overview
lwsapi_app.hpp File Reference
#include <memory>
#include <cstring>
#include "lwip/pbuf.h"
#include "lwip/mem.h"
#include "dbg.h"

Go to the source code of this file.

Classes

class  LwIPClass
 
class  IHeader
 Headers list. Creator is responsible to clean the values. More...
 
class  NumberHeader
 NumberHeader is for number headers like Content-Length. More...
 
class  ConstHeader
 ConstHeader only point to const chars defined in code. More...
 
class  DynamicsHeader
 DynamicsHeader store it's value to LwIP memory pool. More...
 
class  Environment
 Environment struct like as WSGI environment as possible could be. More...
 
struct  Message_t
 Message which must be returned from coroutine generator. More...
 
class  IResponse
 

Macros

#define MAX_HTTP_REQUEST   1024
 LwIP WSAPI C/C++ implementation. More...
 
#define METHOD_LENGTH   10
 maximum method length (PROPPATCH) from WebDAV + \0 More...
 
#define URI_LENGTH   64
 maximum length of request uri More...
 
#define lwsapi_dbg   _dbg
 
#define lwsapi_error   _dbg
 
#define lwsapi_free(arg)
 

Typedefs

typedef IResponse::unique_ptr_t() application_fn(Environment &env)
 application_fn typedef, which is called in tcp_recv callback. More...
 
typedef IHeader *(* header_factory_fn) (const char *key, const char *value, size_t value_length)
 
typedef IHeader *() request_header_fn(const char *key, size_t key_length, const char *value, size_t value_length)
 This factory is used for parsing input headers. More...
 

Functions

IResponse::unique_ptr_t application (Environment &env)
 Define of application functions. More...
 
IHeaderconst_header_factory (const char *key, const char *value, size_t value_length)
 Return new ConstHeader. More...
 
IHeaderdynamics_header_factory (const char *key, const char *value, size_t value_length)
 Return new DynamicsHeader. More...
 
IHeadernumber_header_factory (const char *key, const char *value, size_t value_length)
 Response new NumberHeader. More...
 
IHeaderrequest_header (const char *key, size_t key_length, const char *value, size_t value_length)
 request_header_fn callbacke, which is call from LwIP WSAPI http server More...
 

Macro Definition Documentation

◆ MAX_HTTP_REQUEST

#define MAX_HTTP_REQUEST   1024

LwIP WSAPI C/C++ implementation.

This is C/C++ implementation of Lua's WSAPI or prospal related to WSGI 2.0, for LwIP stack, which is base on tcp callbacks. The interface is like Lua's WSAPI, but extend to context pointers, while C don't have coroutines, and LwIP could work on more systems, so context switching could not be good idea.

+-----—+ +-------------------------------—+ | |Thread X| | LwIP Thread | | | | | +----------------------------—+| | | | | |LwIP || | | | | |+----------------—+ +--—+ || | +----------—+ | | | || WSAPI HTTP Server <--> Eth <-----—> HTTP Client | | | | |+-------—^-----—+ +--—+ || | +----------—+ | | | +--------—|----------------—+| | |+---—+| | +--------—v---------—+ | | || Data <-----—> WSAPI HTTP Application | | | |+---—+| | +---------------------—+ | | +-----—+ +-------------------------------—+ | maximum of HTTP request without payload

◆ METHOD_LENGTH

#define METHOD_LENGTH   10

maximum method length (PROPPATCH) from WebDAV + \0

◆ URI_LENGTH

#define URI_LENGTH   64

maximum length of request uri

◆ lwsapi_dbg

#define lwsapi_dbg   _dbg

◆ lwsapi_error

#define lwsapi_error   _dbg

◆ lwsapi_free

#define lwsapi_free (   arg)
Value:
if (arg != nullptr) { \
mem_free(arg); \
arg = nullptr; \
}

Typedef Documentation

◆ application_fn

typedef IResponse::unique_ptr_t() application_fn(Environment &env)

application_fn typedef, which is called in tcp_recv callback.

This is "WSGI" application handler, which gets reference to Environment structure. This must return IResponse::unique_ptr_t, which must implements generator method to return response content iterative if is needed.

◆ header_factory_fn

typedef IHeader*(* header_factory_fn) (const char *key, const char *value, size_t value_length)

◆ request_header_fn

typedef IHeader*() request_header_fn(const char *key, size_t key_length, const char *value, size_t value_length)

This factory is used for parsing input headers.

request_header_fn is called when each header was detected in request. Application part of this http server must implement this function to decide which header is needed, and which not. All other headers will be ignored.

Returns
Right header object or nullptr when header could be ignore

Function Documentation

◆ application()

IResponse::unique_ptr_t application ( Environment env)

Define of application functions.

Define of application functions.

Parameters
envrequest environment defined in lwsapi.h
argpointer to pointer to internal application reqeust object. This pointer is argument of coroutine_fn funtcions.
Returns
coroutine_fn for response
306  {
307 
308  _dbg("HTTP Request: %s %s", env.method, env.request_uri);
309 
310  if (!strcmp(env.method, "GET") || !strcmp(env.method, "HEAD")) {
311  // Static files
312  for (size_t i = 0; i < (sizeof(files) / sizeof(FileHandler_t)); i++) {
313  if (!strcmp(env.request_uri, files[i].file_name)) {
314  return std::unique_ptr<FileResponse>(new FileResponse(&files[i]));
315  }
316  }
317 
318  if (!strcmp(env.request_uri, "/api/job")) {
319  return api_job(env);
320  } else if (!strcmp(env.request_uri, "/api/printer")) {
321  return api_printer(env);
322  }
323  } else if (!strcmp(env.method, "POST")) {
324  if (!strcmp(env.request_uri, "/post")) {
325  return api_post(env);
326  }
327  }
328  return not_found(env);
329 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ const_header_factory()

IHeader* const_header_factory ( const char *  key,
const char *  value,
size_t  value_length 
)

Return new ConstHeader.

◆ dynamics_header_factory()

IHeader* dynamics_header_factory ( const char *  key,
const char *  value,
size_t  value_length 
)

Return new DynamicsHeader.

441  {
442  return new DynamicsHeader(key, value, value_length);
443 }

◆ number_header_factory()

IHeader* number_header_factory ( const char *  key,
const char *  value,
size_t  value_length 
)

Response new NumberHeader.

446  {
447  return new NumberHeader(key, atoll(value));
448 }

◆ request_header()

IHeader* request_header ( const char *  key,
size_t  key_length,
const char *  value,
size_t  value_length 
)

request_header_fn callbacke, which is call from LwIP WSAPI http server

290  {
291  for (size_t i = 0; i < sizeof(req_headers) / sizeof(header_factory_t); i++) {
292  if (!std::strncmp(req_headers[i].key, key, key_length)) {
293  return req_headers[i].factory(req_headers[i].key, value, value_length);
294  }
295  }
296  return nullptr;
297 }
NumberHeader
NumberHeader is for number headers like Content-Length.
Definition: lwsapi_app.hpp:88
header_factory_t
Definition: connect.cpp:32
api_job
IResponse::unique_ptr_t api_job(Environment &env)
return coroutine_fn for /api/job request
Definition: connect.cpp:211
not_found
IResponse::unique_ptr_t not_found(Environment &env)
return coroutine_fn for Not Found response
Definition: connect.cpp:277
files
static const FileHandler_t files[]
Array of files which could be responed by http.
Definition: connect.cpp:52
api_post
IResponse::unique_ptr_t api_post(Environment &env)
return 200 OK when file is post right
Definition: connect.cpp:257
FileResponse
Class to response any static files defined in files array.
Definition: connect.cpp:84
i
uint8_t i
Definition: screen_test_graph.c:72
req_headers
const header_factory_t req_headers[]
Definition: connect.cpp:37
create_custom_upload_command_CDC.env
env
Definition: create_custom_upload_command_CDC.py:23
header_factory_t::factory
const header_factory_fn factory
Definition: connect.cpp:34
_dbg
#define _dbg(...)
Definition: dbg.h:50
FileHandler_t::file_name
const char * file_name
Definition: connect.cpp:45
FileHandler_t
File handler structure.
Definition: connect.cpp:44
DynamicsHeader
DynamicsHeader store it's value to LwIP memory pool.
Definition: lwsapi_app.hpp:139
api_printer
IResponse::unique_ptr_t api_printer(Environment &env)
return coroutine_fn for /api/printer request
Definition: connect.cpp:226