Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_dynamic_dispatch.c File Reference

Route dispatch for in-process dynamic-app request handling. More...

#include "native_dynamic_dispatch.h"
#include "native_dispatch_table.h"
#include "native_yyjson_helpers.h"
#include "native_support.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Functions

static char * construct_packaged_route_fixture_path (const char *artifact_dir)
 Attempts to locate the route fixture path from the artifact directory.
static char * construct_direct_route_fixture_path (const char *artifact_dir, const char *app_id)
 Builds the framework-data route fixture fallback path.
static char * resolve_route_fixture_path (const char *artifact_dir, const char *app_id)
static int count_segments (const char *path)
 Counts the number of path segments in a pattern or path.
static const char * get_segment (const char *path, int index)
 Extracts the N-th segment (0-indexed) from a path.
static size_t segment_length (const char *start)
 Returns the length of a segment starting at start.
static int route_pattern_specificity_score (const char *pattern)
 Scores a route pattern by match specificity.
static int pattern_matches (const char *pattern, const char *path)
 Tests whether a pattern matches a concrete path.
int native_dynamic_dispatch_match (const char *route_fixture_path, const char *method, const char *path, NativeDynamicRouteMatch **match_out)
 Matches a request path against the route fixture for an app.
void native_dynamic_dispatch_free_match (NativeDynamicRouteMatch *match)
 Frees a route match returned by native_dynamic_dispatch_match().
char * native_dynamic_dispatch_response_file (const char *artifact_dir, const char *method, const char *path)
 Resolves the response artifact file for a matched page route.
char * native_dynamic_dispatch_extract_param (const char *pattern, const char *path, const char *param_name)
 Returns the URL path parameter value extracted from a matched route.
char * native_dynamic_dispatch_resolve_route_fixture (const char *artifact_dir, const char *app_id)
 Resolves the route fixture path from the artifact directory.

Detailed Description

Route dispatch for in-process dynamic-app request handling.

Reads the app's route fixture JSON, matches an incoming request path against declared routes, and extracts parameterised segments.

Definition in file native_dynamic_dispatch.c.

Function Documentation

◆ construct_direct_route_fixture_path()

char * construct_direct_route_fixture_path ( const char * artifact_dir,
const char * app_id )
static

Builds the framework-data route fixture fallback path.

Dynamic-app artifact layouts may also publish route fixtures under data/framework/framework- <app_id>-route-fixture-v1.json.

Parameters
artifact_dirApp artifact directory.
app_idApp identifier used in the framework fixture name.
Returns
Newly allocated path, or NULL on allocation failure.

Definition at line 68 of file native_dynamic_dispatch.c.

References path_join().

Referenced by resolve_route_fixture_path().

◆ construct_packaged_route_fixture_path()

char * construct_packaged_route_fixture_path ( const char * artifact_dir)
static

Attempts to locate the route fixture path from the artifact directory.

Reads the finalized artifact-local app manifest from artifact_dir and extracts the route_fixture path. Falls back to the app-root fixture path when the manifest stores a relative value.

Parameters
artifact_dirApp artifact directory containing the manifest.
Returns
Newly allocated route fixture path, or NULL. Caller must free.

Builds the conventional packaged-app route fixture path.

Packaged app artifacts place the checked route fixture at fixtures/route-fixture.json under the artifact root.

Parameters
artifact_dirApp artifact directory.
Returns
Newly allocated path, or NULL on allocation failure.

Definition at line 41 of file native_dynamic_dispatch.c.

References path_join().

Referenced by resolve_route_fixture_path().

◆ count_segments()

int count_segments ( const char * path)
static

Counts the number of path segments in a pattern or path.

Definition at line 188 of file native_dynamic_dispatch.c.

Referenced by native_dynamic_dispatch_extract_param(), pattern_matches(), and route_pattern_specificity_score().

◆ get_segment()

const char * get_segment ( const char * path,
int index )
static

Extracts the N-th segment (0-indexed) from a path.

Returns
Pointer into the original string (not allocated), or NULL.

Definition at line 217 of file native_dynamic_dispatch.c.

Referenced by native_dynamic_dispatch_extract_param(), pattern_matches(), and route_pattern_specificity_score().

◆ native_dynamic_dispatch_extract_param()

char * native_dynamic_dispatch_extract_param ( const char * pattern,
const char * path,
const char * param_name )

Returns the URL path parameter value extracted from a matched route.

Given a route pattern like /businesses/:business_slug/ and a concrete path /businesses/foo-bar/, this returns "foo-bar".

Parameters
patternRoute pattern from the fixture.
pathConcrete request path.
param_nameName of the parameter segment (without the colon).
Returns
Newly allocated parameter value, or NULL if not found. Caller must free.

Definition at line 518 of file native_dynamic_dispatch.c.

References count_segments(), dup_range(), get_segment(), and segment_length().

Referenced by handle_accept_invite_action_runtime(), handle_action_route(), handle_issue_business_invite_action_runtime(), handle_password_reset_complete_action_runtime(), native_dynamic_runtime_handle_request(), resolve_declared_page_params_json_runtime(), and resolve_invite_accept_page_params_json_runtime().

◆ native_dynamic_dispatch_free_match()

◆ native_dynamic_dispatch_match()

int native_dynamic_dispatch_match ( const char * route_fixture_path,
const char * method,
const char * path,
NativeDynamicRouteMatch ** match_out )

Matches a request path against the route fixture for an app.

Reads route_fixture_path and walks the route list looking for a match. Parameterised segments (prefixed with : ) match any non-empty segment in the corresponding position.

Parameters
route_fixture_pathFile-system path to the route fixture JSON.
methodHTTP method (GET, POST, etc.).
pathNormalised request path (query string already stripped).
match_outDestination for the allocated match record. Caller must free with native_dynamic_dispatch_free_match().
Returns
0 when a match is found, 1 when no route matches, or a negative value on I/O or parse failure.

Definition at line 365 of file native_dynamic_dispatch.c.

References NativeDynamicRouteMatch::action_id, NativeDynamicRouteMatch::auth_gate, NativeDynamicRouteMatch::kind, NativeDynamicRouteMatch::layout_id, native_dynamic_dispatch_free_match(), native_json_doc_free(), native_json_doc_load_file(), native_json_obj_get(), native_json_obj_get_array(), native_json_obj_get_string_dup(), NativeDynamicRouteMatch::page_id, NativeDynamicRouteMatch::path, pattern_matches(), NativeDynamicRouteMatch::policy_gate, NativeDynamicRouteMatch::route_id, route_pattern_specificity_score(), yyjson_arr_get(), yyjson_arr_size(), yyjson_doc_get_root(), and yyjson_get_str().

Referenced by native_dynamic_runtime_handle_request().

◆ native_dynamic_dispatch_resolve_route_fixture()

char * native_dynamic_dispatch_resolve_route_fixture ( const char * artifact_dir,
const char * app_id )

Resolves the route fixture path from the artifact directory.

Reads the manifest from artifact_dir and returns the file-system path to the route fixture JSON.

Parameters
artifact_dirApp artifact directory.
Returns
Newly allocated route fixture path, or NULL. Caller must free.

Definition at line 565 of file native_dynamic_dispatch.c.

References resolve_route_fixture_path().

Referenced by native_dynamic_runtime_handle_request().

◆ native_dynamic_dispatch_response_file()

char * native_dynamic_dispatch_response_file ( const char * artifact_dir,
const char * method,
const char * path )

Resolves the response artifact file for a matched page route.

Looks up the dispatch table in artifact_dir and returns the pre-built response file for routes whose paths are served as static responses. Returns NULL when no static response file exists for the route.

Parameters
artifact_dirApp artifact directory.
methodHTTP method.
pathNormalised request path.
Returns
Newly allocated file path, or NULL. Caller must free.

Definition at line 503 of file native_dynamic_dispatch.c.

References native_dispatch_response_file_for_request().

Referenced by native_dynamic_runtime_handle_request().

◆ pattern_matches()

int pattern_matches ( const char * pattern,
const char * path )
static

Tests whether a pattern matches a concrete path.

Supports:

  • Exact segment matches (e.g. /accounts/login/ )
  • Parameterised segments prefixed with : (e.g. /businesses/:slug/ )
  • Wildcard segment * (matches any single segment)

Definition at line 307 of file native_dynamic_dispatch.c.

References count_segments(), get_segment(), and segment_length().

Referenced by native_dynamic_dispatch_match().

◆ resolve_route_fixture_path()

◆ route_pattern_specificity_score()

int route_pattern_specificity_score ( const char * pattern)
static

Scores a route pattern by match specificity.

Higher scores mean a more specific route. Exact literal segments outrank parameter segments, and parameter segments outrank wildcards. This lets the dispatcher prefer a static route like /appointments/diagnose/ over a broader parameterized route like /appointments/:appointment_id/ when both patterns match the same request path.

Parameters
patternRoute pattern from the fixture.
Returns
Specificity score, or -1 when the pattern is missing.

Definition at line 269 of file native_dynamic_dispatch.c.

References count_segments(), get_segment(), and segment_length().

Referenced by native_dynamic_dispatch_match().

◆ segment_length()

size_t segment_length ( const char * start)
static

Returns the length of a segment starting at start.

Definition at line 249 of file native_dynamic_dispatch.c.

Referenced by native_dynamic_dispatch_extract_param(), pattern_matches(), and route_pattern_specificity_score().