/** * Utility functions for users and internal use of the HTTP server */ #pragma once #include #include #include "httpd-types.h" // Custom helpers #define streq(a, b) (strcmp((const char*)(a), (const char*)(b)) == 0) #define strneq(a, b, n) (strncmp((const char*)(a), (const char*)(b), (n)) == 0) #define strstarts(a, b) strneq((a), (b), (int)strlen((b))) #define last_char_n(str, n) ((str))[strlen((str)) - (n)] #define last_char(str) last_char_n((str), 1) #ifndef container_of #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) #endif /** * Turn a nibble (0-15) to a hex char. * * Only the bottom 4 bits are considered. * * @param val * @return hex char, uppercase */ char httpdHexNibble(uint8_t val); /** * Turn a hex char into integer * * @param c - char to convert, [0-9a-fA-F] * @return integer value 0-15 */ uint8_t httpdHexVal(char c); /** * Decode a percent-encoded value. * Takes the valLen bytes stored in val, and converts it into at most retLen bytes that * are stored in the ret buffer. Returns the actual amount of bytes used in ret. Also * zero-terminates the ret buffer. * * @param val - the encoded value * @param valLen - length of the encoded value field * @param buff - output buffer, the string will be zero-terminated * @param buffLen - output buffer size * @return */ int httpdUrlDecode(const char *val, size_t valLen, char *buff, size_t buffLen); /** * Find a specific arg in a string of get- or post-data. * Line is the string of post/get-data, arg is the name of the value to find. The * zero-terminated result is written in buff, with at most buffLen bytes used. The * function returns the length of the result, or -1 if the value wasn't found. The * returned string will be urldecoded already. * * @param line - line to parse * @param arg - name of the argument to retrieve * @param[out] buff - output buffer, the string will be zero-terminated * @param buffLen - output buffer size * @return */ int httpdFindArg(const char *line, const char *arg, char *buff, size_t buffLen); /** * Returns a static char* to a mime type for a given url to a file. * * @param url - URL or filename to parse * @return mime type string; NULL if unknown. */ const char *httpdGetMimetype(const char *url); /** * Get mimetype or default * * @param url - URL or filename to parse * @param fallback - fallback mime if none was resolved * @return mime string */ static inline const char *httpdGetMimetypeOr(const char *url, const char *fallback) { const char *mime = httpdGetMimetype(url); if (!mime) { mime = fallback; } return mime; } /** * Turn HTTP method to text * * @param m - method enum * @return text, e.g. GET */ const char *httpdMethodName(httpd_method m); /** * Get text version of a HTTP status code * * @param code - code * @return text, e.g OK or Forbidden */ const char *httpdStatusName(int code);