You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you call `json_parse_file`, it looks at the very start of your file. Since a JSON file can be a single number, a string, or a large object, Parson always returns a **`JSON_Value*`**.
86
86
87
-
- If your file starts with `{`, the top-level `JSON_Value` contains a `JSON_Object`.
88
-
- If your file starts with `[`, the top-level `JSON_Value` contains a `JSON_Array`.
87
+
So after getting the `JSON_Value` struct object from `json_parse_file()`, methods like `json_value_get_object()`, `json_value_get_array()`, etc.. are used to resolve it into the actual object or array. In our case we use `json_value_get_object()` as our config file `xps_config.json` starts with `{` and ends with `}` which implies it is an object.
89
88
90
89
:::info NOTE
91
-
Since a JSON file can start with any valid data type (an object, an array, a string, or a number), the parser returns a generic `JSON_Value` struct object. You must then use other Parson methods to "unpack" the specific data structure you expect.
90
+
Since a JSON file can start with any valid data type (an object, an array, a string, or a number), the parser returns a generic `JSON_Value` struct object. We then must use the appropriate method to resolve it into the intended object or array.
Copy file name to clipboardExpand all lines: docs/roadmap/phase-2/stage-16.md
+58-35Lines changed: 58 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,9 +44,7 @@ We are also introducing the redirecting server and re-introducing the reverse pr
44
44
45
45
A new module `xps_config` is added for reading the JSON configuration file and storing the parsed data in the structs. It also implements a lookup of the configuration to determine the appropriate server and route for an incoming request. It also matchs the listeners and hostnames.
46
46
47
-
The session module is updated to incorporate the lookup of configuration while processing the request. The appropriate functionality is executed with respsect to the request type obtained from the lookup. The redirect server and reverse proxy server are implemented here.
48
-
49
-
The `main.c` is updated to create multiple cores. Each of the listeners mentioned in the configuration file is duplicated and added to all the cores created.
47
+
The session module is updated to incorporate the lookup of configuration while processing the request. The appropriate functionality is executed with respsect to the request type obtained from the lookup. The redirect server and reverse proxy server are implemented in this stage.
50
48
51
49
## Implementation
52
50
@@ -95,7 +93,7 @@ This will create a parson directory inside `expserver/src/lib/` and download all
The names of the structs and its fields are intuitive, try to go through each and understand its use.
223
-
224
-
The struct `xps_config_s` represents the overall configuration of the server.
225
-
226
-
The struct `xps_config_server_s` describes individual server configurations.
227
215
228
-
The struct `xps_config_lookup_s` represents a lookup result from configuration where `xps_req_type_t type` indicates thr type of request (REQ_FILE_SERVE, REQ_REVERSE_PROXY, etc.).
216
+
The names of the structs and its fields are intuitive, try to go through each and understand its use.
229
217
230
-
Fields `file_path`, `dir_path`, `file_start` used by a file server, `upstream` used by a reverse proxy server and `http_status_code`, `redirect_url` used by a redirect.
218
+
#### `struct xps_config_s`
219
+
This is the top-level structure that represents the entire server configuration parsed from the JSON file.
220
+
-`config_path`: Path to the JSON configuration file.
221
+
-`server_name`: Name of the eXpServer instance.
222
+
-`servers`: A list of individual server configurations (`xps_config_server_t`).
223
+
-`_all_listeners`: A consolidated list of all unique listeners across all servers.
224
+
-`_config_json`: Pointer to the internal Parson JSON value.
225
+
226
+
#### `struct xps_config_server_s`
227
+
Encapsulates configuration for an individual server block.
228
+
-`listeners`: A list of `xps_config_listener_t` defining host/port bindings.
229
+
-`hostnames`: A list of virtual hostnames associated with this server.
230
+
-`routes`: A list of `xps_config_route_t` defining request handling rules.
231
+
232
+
#### `struct xps_config_route_s`
233
+
Defines how a specific URL path should be handled.
234
+
-`req_path`: The URL prefix used for route matching (e.g., `/api`).
235
+
-`type`: The behavior type (`file_serve`, `redirect`, `reverse_proxy`, etc.).
236
+
-`dir_path`: The root directory on disk for serving files.
237
+
-`index`: A list of default files to try when a directory is requested (e.g., `index.html`).
238
+
-`upstreams`: A list of backend servers for load balancing.
239
+
-`http_status_code`: The HTTP status returned for redirects (e.g., `301`, `302`).
240
+
-`redirect_url`: Target destination for redirection.
241
+
242
+
#### `struct xps_config_lookup_s`
243
+
This structure represents the **result** of a configuration lookup for a specific request.
244
+
-`type`: The resolved request type (e.g., `REQ_FILE_SERVE`, `REQ_REVERSE_PROXY`, etc.).
245
+
-`file_path`: Absolute path to a specific file found on disk.
246
+
-`dir_path`: Absolute path to a directory being accessed.
247
+
-`upstream`: The specific backend server selected for proxying.
248
+
-`http_status_code`: The status code for the redirect response.
249
+
-`redirect_url`: Final destination URL for the redirect.
250
+
-`ip_whitelist`: Vector of allowed IP addresses for this route.
251
+
-`ip_blacklist`: Vector of blocked IP addresses for this route.
/*get host,keep_alive(connection),accept encoding,pathname from http_req*/
288
+
/*get host,accept encoding,pathname from http_req*/
268
289
// Step 1: Find matching server block
269
290
int target_server_index = -1;
270
291
for (int i = 0;/*fill this*/; i++) {
@@ -406,7 +427,9 @@ if (lookup->type == REQ_FILE_SERVE) {
406
427
407
428
### Listener Module - Modifications
408
429
409
-
- Core is not used while creating listeners as now there are multiple cores attached to the config.
430
+
- Core is not used while creating listeners as now we are creating listeners in `main.c`
431
+
- In `xps_listener_create()` remove the `xps_core_t *core` argument and remove the code `listener->core = core` from the function as we will be setting it in `main.c`
432
+
410
433
- Attaching the listener to the event loop and pushing to the listeners list of core is already done during the configuration set-up.
0 commit comments