Skip to content

Commit 42997b8

Browse files
authored
Merge branch 'master' into master
2 parents 8a9f73c + bb3cc3c commit 42997b8

2 files changed

Lines changed: 61 additions & 39 deletions

File tree

docs/guides/references/parson.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This is the "skeleton" of every JSON element. It tells Parson what **type** the
2727

2828
```c
2929
struct json_value_t {
30-
...
30+
...
3131
JSON_Value_Type type; // Is it a String, Number, Object, etc.?
3232
JSON_Value_Value value; // The actual data (a Union)
3333
};
@@ -84,11 +84,10 @@ JSON_Value *json_parse_file(const char *filename);
8484
8585
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*`**.
8686
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.
8988
9089
:::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.
9291
:::
9392
9493
### `json_value_get_object()`

docs/roadmap/phase-2/stage-16.md

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ We are also introducing the redirecting server and re-introducing the reverse pr
4444

4545
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.
4646

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.
5048

5149
## Implementation
5250

@@ -95,7 +93,7 @@ This will create a parson directory inside `expserver/src/lib/` and download all
9593
"index": ["index.html"]
9694
},
9795
{
98-
"req_path": "/hello",
96+
"req_path": "/redirect",
9997
"type": "redirect",
10098
"http_status_code": 302,
10199
"redirect_url": "http://localhost:8002/"
@@ -174,7 +172,6 @@ struct xps_config_route_s {
174172
vec_void_t upstreams;
175173
u_int http_status_code;
176174
const char *redirect_url;
177-
bool keep_alive;
178175
};
179176

180177
typedef enum xps_req_type_e {
@@ -191,9 +188,6 @@ struct xps_config_lookup_s {
191188
/* file_serve */
192189
char *file_path; // absolute path
193190
char *dir_path; // absolute path
194-
long file_start; // parse range header
195-
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
196-
long file_end;
197191

198192
/* reverse_proxy */
199193
const char *upstream;
@@ -203,7 +197,6 @@ struct xps_config_lookup_s {
203197
const char *redirect_url;
204198

205199
/* common */
206-
bool keep_alive;
207200
vec_void_t ip_whitelist;
208201
vec_void_t ip_blacklist;
209202
};
@@ -219,15 +212,43 @@ void xps_config_lookup_destroy(xps_config_lookup_t *config_lookup, xps_core_t *c
219212

220213
:::
221214

222-
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.
227215

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.
229217

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.
231252

232253
##### xps_config.c
233254

@@ -264,7 +285,7 @@ xps_config_lookup_t *xps_config_lookup(xps_config_t *config, xps_http_req_t *htt
264285
xps_connection_t *client, int *error) {
265286
/*assert*/
266287
*error = E_FAIL;
267-
/*get host,keep_alive(connection),accept encoding,pathname from http_req*/
288+
/*get host,accept encoding,pathname from http_req*/
268289
// Step 1: Find matching server block
269290
int target_server_index = -1;
270291
for (int i = 0;/*fill this*/; i++) {
@@ -406,7 +427,9 @@ if (lookup->type == REQ_FILE_SERVE) {
406427

407428
### Listener Module - Modifications
408429

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+
410433
- Attaching the listener to the event loop and pushing to the listeners list of core is already done during the configuration set-up.
411434

412435
### main.c
@@ -443,25 +466,25 @@ void sigint_handler(int signum) {
443466
### Additional utilities to be added
444467
445468
- Cliargs : To handle and store command-line arguments related to the configuration file path.
446-
:::details **expserver/src/utils/xps_cliargs.h**
469+
:::details **expserver/src/utils/xps_cliargs.h**
447470
448-
```c
449-
#ifndef XPS_CLIARGS_H
450-
#define XPS_CLIARGS_H
471+
```c
472+
#ifndef XPS_CLIARGS_H
473+
#define XPS_CLIARGS_H
451474
452-
#include "../xps.h"
475+
#include "../xps.h"
453476
454-
struct xps_cliargs_s {
455-
char *config_path;
456-
};
477+
struct xps_cliargs_s {
478+
char *config_path;
479+
};
457480
458-
xps_cliargs_t *xps_cliargs_create(int argc, char *argv[]);
459-
void xps_cliargs_destroy(xps_cliargs_t *cilargs);
481+
xps_cliargs_t *xps_cliargs_create(int argc, char *argv[]);
482+
void xps_cliargs_destroy(xps_cliargs_t *cilargs);
460483
461-
#endif
462-
```
484+
#endif
485+
```
463486

464-
:::
487+
:::
465488

466489
:::details **expserver/src/utils/xps_cliargs.c**
467490

@@ -500,9 +523,9 @@ void xps_cliargs_destroy(xps_cliargs_t *cliargs) {
500523
:::
501524
502525
- Utility functions required for checking directory, file, absolute path.
503-
:::details **expserver/src/utils/xps_utils.c** {#xps-utils-c}
526+
:::details **expserver/src/utils/xps_utils.c** {#xps-utils-c}
504527
505-
```c
528+
```c
506529
507530
bool str_starts_with(const char *str, const char *prefix) {
508531
assert(str != NULL);
@@ -575,9 +598,9 @@ void xps_cliargs_destroy(xps_cliargs_t *cliargs) {
575598
assert(path != NULL);
576599
return path[0] == '/';
577600
}
578-
```
601+
```
579602

580-
:::
603+
:::
581604

582605
- Also update `xps_utils.h` accordingly.
583606

0 commit comments

Comments
 (0)