Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_http_router.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nilorea/n_common.h"
#include "nilorea/n_http_router.h"
#include "nilorea/n_log.h"
Include dependency graph for ex_http_router.c:

Go to the source code of this file.

Functions

static void expect_route (const N_ROUTER *r, n_http_req_method method, const char *path, const char *want, N_ROUTE_MATCH *m)
 route path with method and expect a route data tag (NULL: not found)
static void expect_true (const char *what, int ok)
 count a failed check
static int h (void *ctx, const N_ROUTE_MATCH *m)
 a handler that does nothing (the test only routes)
int main (int argc, char **argv)
 run the checks
static int param_is (const N_ROUTE_MATCH *m, const char *name, const char *want)
 param name of the last match equals want
static int process_args (int argc, char **argv)
 read -V
static void test_checks (void)
 the start-up checks
static void test_match (const N_ROUTER *r)
 matching on the blog's tables

Variables

static const N_ROUTE ADMIN []
 a few admin routes
static int failures = 0
 failed checks
static int log_level = 3
 log level from -V
static const N_ROUTE PUBLIC []
 the public routes of the Nilorea blog, in a deliberately unhelpful order: the catch-all "/{slug}/" comes first

Function Documentation

◆ expect_route()

void expect_route ( const N_ROUTER * r,
n_http_req_method method,
const char * path,
const char * want,
N_ROUTE_MATCH * m )
static

route path with method and expect a route data tag (NULL: not found)

Parameters
rrouter
methodmethod
paththe path
wantexpected data tag
mreceives the match
Examples
ex_http_router.c.

Definition at line 114 of file ex_http_router.c.

References N_ROUTE::data, failures, LOG_ERR, n_log, N_ROUTER_FOUND, n_router_match(), N_ROUTER_NOT_FOUND, and N_ROUTE_MATCH::route.

Referenced by test_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expect_true()

void expect_true ( const char * what,
int ok )
static

count a failed check

Parameters
whatthe check
okits result

Definition at line 47 of file ex_http_router.c.

References failures, LOG_ERR, LOG_INFO, and n_log.

◆ h()

int h ( void * ctx,
const N_ROUTE_MATCH * m )
static

a handler that does nothing (the test only routes)

Parameters
ctxunused
munused
Returns
0
Examples
ex_accept_pool_server.c, ex_atlas.c, ex_cookies.c, ex_digest.c, ex_fluid.c, ex_gui_button_keys.c, ex_gui_combobox_items.c, ex_gui_curve.c, ex_gui_detach.c, ex_gui_display_fallback.c, ex_gui_grid.c, ex_gui_isometric.c, ex_gui_label_lines.c, ex_gui_multiwin.c, ex_gui_particles.c, ex_gui_popup.c, ex_http2.c, ex_http2_server.c, ex_http_cookie.c, ex_http_response.c, ex_http_router.c, ex_iso_astar.c, ex_network_alpn.c, ex_pth.c, and ex_url_safe.c.

Definition at line 77 of file ex_http_router.c.

Referenced by _a4_item_at(), _a4_item_rect(), _civil_to_time(), _cookie_scan(), _curve_draw(), _draw_label_lines(), _draw_rows_scrollbar(), _draw_scrollbar_thumb(), _draw_textarea(), _draw_themed_rect(), _global_scroll_needs(), _label_lines_char_at(), _label_lines_top(), _mp_part_headers(), _n_dc_walk(), _n_gui_apply_display_size(), _new_widget(), _ngui_meas_slot(), _popup_row_at(), _resp_cat(), _sha256_block(), _suggest_panel_rect(), _text_dims(), _tile_covers_height(), _wheel_geometry(), _win_auto_sb_rects(), background(), build_panel(), count_pixels_differing(), demo_astar(), demo_astar_standalone(), demo_dead_reckoning(), demo_iso_engine(), draw_background(), draw_cell(), draw_into(), draw_particle(), element_value(), find_hdr(), fmix32(), gfx_mode(), h2_on_request(), handle_inline_and_count(), hdr_has(), head_is(), heap_free(), heap_grow(), heap_new(), heap_pop(), heap_push(), ink(), is_http_url(), iso_map_build_draw_order(), iso_map_draw(), iso_map_get_visible_edges(), iso_map_get_visible_edges_segment(), iso_map_set_height(), iso_map_smooth_corner_heights(), iso_map_to_screen(), iso_map_to_screen_f(), iso_screen_to_map_height(), iso_screen_to_map_height_f(), lexMenuRun(), main(), main(), main(), make_png(), match_heading(), md_run(), md_table(), n_entropy_positions(), n_entropy_shannon(), n_gui_add_button(), n_gui_add_button_bitmap(), n_gui_add_checkbox(), n_gui_add_combobox(), n_gui_add_custom(), n_gui_add_datagrid(), n_gui_add_dropmenu(), n_gui_add_hexview(), n_gui_add_image(), n_gui_add_label(), n_gui_add_label_link(), n_gui_add_listbox(), n_gui_add_markdownview(), n_gui_add_progressbar(), n_gui_add_radiolist(), n_gui_add_scrollbar(), n_gui_add_slider(), n_gui_add_splitpane(), n_gui_add_syntaxview(), n_gui_add_textarea(), n_gui_add_toggle_button(), n_gui_add_vslider(), n_gui_add_window(), n_gui_colorwheel_hue(), n_gui_colorwheel_new(), n_gui_curve_eval_points(), n_gui_curve_new(), n_gui_draw_color_swatch(), n_gui_grid_cell_rect(), n_gui_grid_set_rect(), n_gui_label_fits(), n_gui_set_display_size(), n_gui_set_virtual_size(), n_gui_tree_create(), n_gui_widget_screen_rect(), n_gui_widget_set_rect(), n_gui_window_button_rect(), n_gui_window_set_rect(), n_gui_window_set_rect_axes(), n_http_request_header(), n_http_request_header_count(), n_http_response_head(), n_time_parse_http_date(), n_time_parse_rfc3339(), n_wrap_index_at(), on_ghost_toggle(), on_player_toggle(), on_request(), peer_headers(), print_map_ascii(), randomize_map(), set_gfx_mode(), stretch_sprite(), test_checks(), test_headers(), test_read(), test_sha256_hex(), worker_handle_client(), wrap_hit_line(), and write_zip().

◆ main()

int main ( int argc,
char ** argv )

run the checks

Parameters
argcargument count
argvarguments
Returns
0 when every check passes

Definition at line 273 of file ex_http_router.c.

References ADMIN, expect_true(), failures, LOG_ERR, log_level, LOG_NOTICE, n_log, n_router_add(), n_router_check(), n_router_count(), n_router_free(), n_router_new(), n_router_route(), process_args(), PUBLIC, set_log_level(), test_checks(), and test_match().

Here is the call graph for this function:

◆ param_is()

int param_is ( const N_ROUTE_MATCH * m,
const char * name,
const char * want )
static

param name of the last match equals want

Parameters
mthe match
nameplaceholder
wantexpected decoded value
Returns
non-zero when equal
Examples
ex_http_router.c.

Definition at line 129 of file ex_http_router.c.

References n_route_param().

Referenced by test_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_args()

int process_args ( int argc,
char ** argv )
static

read -V

Parameters
argcargument count
argvarguments
Returns
0, or -1 on a bad argument

Definition at line 60 of file ex_http_router.c.

References log_level.

◆ test_checks()

void test_checks ( void )
static

the start-up checks

Examples
ex_http_router.c.

Definition at line 208 of file ex_http_router.c.

References expect_true(), failures, h(), LOG_ERR, n_log, N_ROUTE_GET, N_ROUTE_HEAD, N_ROUTE_POST, n_router_add(), n_router_check(), n_router_count(), n_router_free(), and n_router_new().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_match()

void test_match ( const N_ROUTER * r)
static

matching on the blog's tables

Examples
ex_http_router.c.

Definition at line 136 of file ex_http_router.c.

References expect_route(), expect_true(), N_HTTP_REQ_GET, N_HTTP_REQ_HEAD, N_HTTP_REQ_POST, N_ROUTE_GET, N_ROUTE_HEAD, n_route_param(), N_ROUTE_POST, n_router_allow_header(), N_ROUTER_BAD_REQUEST, n_router_match(), N_ROUTER_METHOD_NOT_ALLOWED, N_ROUTER_NOT_FOUND, N_ROUTE_MATCH::nb_params, and param_is().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ADMIN

const N_ROUTE ADMIN[]
static
Initial value:
= {
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/admin/login", h, "login-form"},
{ (1u << (unsigned)(N_HTTP_REQ_POST)) , "/admin/login", h, "login"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) | (1u << (unsigned)(N_HTTP_REQ_POST)) , "/admin/posts/{id:int9}", h, "post-edit"},
{ (1u << (unsigned)(N_HTTP_REQ_POST)) , "/admin/posts/{id:int9}/trash", h, "post-trash"},
}
static int h(void *ctx, const N_ROUTE_MATCH *m)
a handler that does nothing (the test only routes)
@ N_HTTP_REQ_POST
Definition n_http_req.h:79
@ N_HTTP_REQ_GET
Definition n_http_req.h:78

a few admin routes

Examples
ex_http_router.c.

Definition at line 101 of file ex_http_router.c.

Referenced by main().

◆ failures

int failures = 0
static

failed checks

Definition at line 42 of file ex_http_router.c.

◆ log_level

int log_level = 3
static

log level from -V

Definition at line 40 of file ex_http_router.c.

◆ PUBLIC

const N_ROUTE PUBLIC[]
static
Initial value:
= {
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/{slug:slug}/", h, "page"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/", h, "home"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/page/{n:int6}/", h, "home-paged"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/category/{slug:slug}/", h, "category"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/category/{slug:slug}/page/{n:int6}/", h, "category-paged"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/{y:d4}/{m:d2}/", h, "month"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/{y:d4}/{m:d2}/{d:d2}/{slug:slug}/", h, "post"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/feed/", h, "feed"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/sitemap.xml", h, "sitemap"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/media/{id:hex16}/{name:seg}", h, "media"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/media/{id:hex16}/{w:int4}/{name:seg}", h, "variant"},
{ (1u << (unsigned)(N_HTTP_REQ_GET)) , "/wp-content/uploads/{path:rest}", h, "legacy-upload"},
}

the public routes of the Nilorea blog, in a deliberately unhelpful order: the catch-all "/{slug}/" comes first

Examples
ex_http_router.c.

Definition at line 85 of file ex_http_router.c.

Referenced by main().