Pharos
0.7.23
Modern Web Framework & Web App Hosting Service.
Toggle main menu visibility
Loading...
Searching...
No Matches
native_dispatch_table.c
Go to the documentation of this file.
1
/**
2
* @file
3
* @brief Matches API request paths against the dispatch table and resolves the response artifact file to serve.
4
*/
5
6
#include "
native_dispatch_table.h
"
7
8
#include "
native_json_config.h
"
9
#include "
native_support.h
"
10
11
#include <stdlib.h>
12
#include <string.h>
13
14
#ifdef _WIN32
15
#define native_dispatch_strdup _strdup
16
#else
17
#define native_dispatch_strdup strdup
18
#endif
19
20
int
native_dispatch_route_pattern_matches
(
const
char
*pattern,
const
char
*path) {
21
const
char
*pattern_cursor = pattern != NULL ? pattern :
""
;
22
const
char
*path_cursor = path != NULL ? path :
""
;
23
while
(*pattern_cursor !=
'\0'
&& *path_cursor !=
'\0'
) {
24
if
(*pattern_cursor ==
'{'
) {
25
const
char
*pattern_end = strchr(pattern_cursor,
'}'
);
26
if
(pattern_end == NULL) {
27
return
0;
28
}
29
if
(*path_cursor ==
'/'
|| *path_cursor ==
'\0'
) {
30
return
0;
31
}
32
while
(*path_cursor !=
'\0'
&& *path_cursor !=
'/'
) {
33
path_cursor++;
34
}
35
pattern_cursor = pattern_end + 1;
36
continue
;
37
}
38
if
(*pattern_cursor != *path_cursor) {
39
return
0;
40
}
41
pattern_cursor++;
42
path_cursor++;
43
}
44
return
*pattern_cursor ==
'\0'
&& *path_cursor ==
'\0'
;
45
}
46
47
char
*
native_dispatch_response_file_for_request
(
const
char
*root,
const
char
*method,
const
char
*path) {
48
char
*dispatch_path = NULL;
49
char
*dispatch_text = NULL;
50
char
*response_file = NULL;
51
const
char
*cursor;
52
if
(root == NULL || method == NULL || path == NULL || !
starts_with
(path,
"/api/"
)) {
53
return
NULL;
54
}
55
dispatch_path =
path_join
(root,
"dispatch-table.json"
);
56
if
(dispatch_path == NULL) {
57
return
NULL;
58
}
59
dispatch_text =
read_file_text
(dispatch_path);
60
free(dispatch_path);
61
if
(dispatch_text == NULL) {
62
return
NULL;
63
}
64
cursor = dispatch_text;
65
while
((cursor = strstr(cursor,
"\"method\""
)) != NULL) {
66
char
*entry_method =
json_find_string_after
(dispatch_text,
"method"
, cursor);
67
char
*entry_path =
json_find_string_after
(dispatch_text,
"path"
, cursor);
68
char
*entry_response =
json_find_string_after
(dispatch_text,
"response"
, cursor);
69
if
(entry_method != NULL && entry_path != NULL && entry_response != NULL &&
70
streq
(entry_method, method) &&
native_dispatch_route_pattern_matches
(entry_path, path)) {
71
response_file =
native_dispatch_strdup
(entry_response);
72
free(entry_method);
73
free(entry_path);
74
free(entry_response);
75
break
;
76
}
77
free(entry_method);
78
free(entry_path);
79
free(entry_response);
80
cursor++;
81
}
82
free(dispatch_text);
83
return
response_file;
84
}
native_dispatch_response_file_for_request
char * native_dispatch_response_file_for_request(const char *root, const char *method, const char *path)
Resolves the response artifact file for one request using the dispatch table.
Definition
native_dispatch_table.c:47
native_dispatch_route_pattern_matches
int native_dispatch_route_pattern_matches(const char *pattern, const char *path)
Tests whether a request path matches one dispatch-table route pattern.
Definition
native_dispatch_table.c:20
native_dispatch_strdup
#define native_dispatch_strdup
Definition
native_dispatch_table.c:17
native_dispatch_table.h
Declares dispatch-table request path helpers.
json_find_string_after
char * json_find_string_after(const char *text, const char *key, const char *start_at)
Finds a JSON string value for a key after a given scan position.
Definition
native_json_config.c:20
native_json_config.h
Declares lightweight JSON and conf value lookup helpers.
path_join
char * path_join(const char *left, const char *right)
Joins two path segments using the native path separator.
Definition
native_support.c:115
streq
int streq(const char *a, const char *b)
Tests whether two strings are exactly equal.
Definition
native_support.c:111
read_file_text
char * read_file_text(const char *path)
Reads an entire file into a newly allocated text buffer.
Definition
native_support.c:318
starts_with
int starts_with(const char *value, const char *prefix)
Tests whether one string starts with another string.
Definition
native_support.c:107
native_support.h
Declares shared low-level support helpers used across the Pharos native runtime.
src
native_runtime
c
native_dispatch_table.c
Generated by
1.17.0