Pharos 0.7.23
Modern Web Framework & Web App Hosting Service.
Loading...
Searching...
No Matches
native_request_path.c
Go to the documentation of this file.
1/**
2 * @file
3 * @brief Normalizes request targets by stripping query strings and remapping them under an app mount path.
4 */
5
7
8#include "native_support.h"
9
10#include <stdlib.h>
11#include <string.h>
12
13#ifdef _WIN32
14#define native_request_path_strdup _strdup
15#else
16#define native_request_path_strdup strdup
17#endif
18
19char *native_request_path_without_query(const char *target) {
20 const char *path = target != NULL ? target : "/";
21 const char *query = strchr(path, '?');
22 size_t len = query != NULL ? (size_t)(query - path) : strlen(path);
23 return dup_range(path, len);
24}
25
26char *native_request_target_for_mount(const char *mount_path, const char *target) {
27 const char *mount = mount_path != NULL ? mount_path : "";
28 const char *path = target != NULL ? target : "/";
29 const char *query = strchr(path, '?');
30 size_t path_len = query != NULL ? (size_t)(query - path) : strlen(path);
31 size_t mount_len = strlen(mount);
32 const char *trimmed = path;
33 char *result;
34
35 if (mount_len > 0 && strncmp(path, mount, mount_len) == 0) {
36 trimmed = path + mount_len;
37 if (*trimmed == '\0') {
38 trimmed = "/";
39 }
40 }
41 if (*trimmed == '\0') {
42 trimmed = "/";
43 }
44 if (query != NULL) {
45 size_t trimmed_len = path_len - (size_t)(trimmed - path);
46 result = (char *)malloc(trimmed_len + strlen(query) + 1);
47 if (result == NULL) {
48 return NULL;
49 }
50 memcpy(result, trimmed, trimmed_len);
51 memcpy(result + trimmed_len, query, strlen(query) + 1);
52 return result;
53 }
54 return native_request_path_strdup(trimmed);
55}
char * native_request_path_without_query(const char *target)
Removes the query string component from a request target.
char * native_request_target_for_mount(const char *mount_path, const char *target)
Remaps a request target so it is relative to an app mount path.
#define native_request_path_strdup
Declares request target normalization helpers.
char * dup_range(const char *start, size_t len)
Duplicates a byte range into a newly allocated NUL-terminated string.
Declares shared low-level support helpers used across the Pharos native runtime.
const char size_t len
Definition yyjson.h:8364