-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorRestStateInit.c
More file actions
146 lines (114 loc) · 4.44 KB
/
Copy pathcorRestStateInit.c
File metadata and controls
146 lines (114 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// FILE corRestStateInit.c
//
// AUTHOR Ken Zangelin
//
// Copyright 2026 Seamware
// SPDX-License-Identifier: Apache-2.0
//
#include <stdlib.h> // free, atoi
#include <string.h> // memset, strdup, strchr, strlen
#include "kalloc/kaBufferInit.h" // kaBufferInit
#include "kalloc/kaBufferReset.h" // kaBufferReset
#include "kjson/kjBufferCreate.h" // kjBufferCreate
#include "corRest/CorRestState.h" // CorRestState, corRest
#include "corRest/CorRestVerb.h" // corRestVerbFromString
#include "corRest/corRestStateInit.h" // Own interface
// -----------------------------------------------------------------------------
//
// corRest backing storage — the per-thread fallback object and the current-
// state pointer the `corRest` macro dereferences (see CorRestState.h). corRestP
// stays NULL until first access on a thread, then auto-binds to corRestFallback.
//
__thread CorRestState corRestFallback;
__thread CorRestState* corRestP = NULL;
// -----------------------------------------------------------------------------
//
// External: default pretty-print setting from corRestHooks.c
//
extern int corRestDefaultPrettySpaces;
// -----------------------------------------------------------------------------
//
// corRestStateInit -
//
void corRestStateInit(void* connection, const char* url, const char* method)
{
memset(&corRest, 0, sizeof(CorRestState));
corRest.connection = connection;
// Initialize kalloc pool (inline buffer first, then malloc-based overflow).
// 256KB grow chunks: keeps single-shot renderings of ~1000-entity responses
// inside one block. kaAlloc silently returns NULL for any single request
// >= chunk size, so this also caps the max single allocation.
kaBufferInit(&corRest.kalloc, corRest.kallocBuffer, sizeof(corRest.kallocBuffer), 256 * 1024, NULL, "corRest");
// Initialize kjson with kalloc
corRest.kjsonP = kjBufferCreate(&corRest.kjson, &corRest.kalloc);
// Verb
corRest.in.verb = corRestVerbFromString(method);
corRest.in.verbString = (char*) method;
// URL path (allocated via strdup - freed in corRestStateRelease)
corRest.in.urlPath = strdup(url);
// Separate query string from URL path
char* qMark = strchr(corRest.in.urlPath, '?');
if (qMark != NULL)
{
*qMark = 0;
corRest.in.urlParams = qMark + 1;
}
corRestUrlPathNormalize();
// Initialize URI param dynamic array (starts with inline slots)
corRest.in.uriParamV = corRest.in.uriParams;
corRest.in.uriParamCount = 0;
corRest.in.uriParamSize = COR_REST_INITIAL_KV_SLOTS;
corRest.in.uriParamMask = 0;
// Initialize HTTP header dynamic array (starts with inline slots)
corRest.in.httpHeaderV = corRest.in.httpHeaders;
corRest.in.httpHeaderCount = 0;
corRest.in.httpHeaderSize = COR_REST_INITIAL_KV_SLOTS;
// Pretty-print default
corRest.in.prettySpaces = corRestDefaultPrettySpaces;
// Defaults for response
corRest.out.httpStatusCode = 200;
corRest.out.contentType = (char*) corMimeString(CorMimeJson);
corRest.out.headerV = corRest.out.headers;
corRest.out.headerCount = 0;
corRest.out.headerSize = COR_REST_INITIAL_KV_SLOTS;
// Payload accumulation
corRest.in.payload = NULL;
corRest.in.payloadSize = 0;
corRest.payloadBufSize = 0;
}
// -----------------------------------------------------------------------------
//
// corRestUrlPathNormalize -
//
// Strip a single trailing '/' from the path (except the root "/" itself).
// Routes are registered without a trailing slash; clients that send one
// (e.g. ETSI test suite: POST /ngsi-ld/v1/entities/) would otherwise
// 404 against an exact-match service lookup.
//
void corRestUrlPathNormalize(void)
{
if (corRest.in.urlPath == NULL)
return;
corRest.in.urlPathLen = strlen(corRest.in.urlPath);
if (corRest.in.urlPathLen > 1 && corRest.in.urlPath[corRest.in.urlPathLen - 1] == '/')
{
corRest.in.urlPath[corRest.in.urlPathLen - 1] = 0;
corRest.in.urlPathLen--;
}
}
// -----------------------------------------------------------------------------
//
// corRestStateRelease - release per-request resources
//
void corRestStateRelease(void)
{
// Free the URL path (strdup'd in corRestStateInit)
if (corRest.in.urlPath != NULL)
{
free(corRest.in.urlPath);
corRest.in.urlPath = NULL;
}
// Bulk-free all kalloc allocations
kaBufferReset(&corRest.kalloc, KFALSE);
}