Skip to content

Commit 98441e0

Browse files
committed
Add host and scheme when building request, fix log creation
1 parent f473a96 commit 98441e0

File tree

4 files changed

+38
-73
lines changed

4 files changed

+38
-73
lines changed

src/mod_redirectionio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ static int redirectionio_match_handler(request_rec *r) {
127127
return DECLINED;
128128
}
129129

130+
ctx->request = NULL;
130131
ctx->action = NULL;
132+
ctx->response_headers = NULL;
131133
ctx->body_filter = NULL;
132134

133135
ap_set_module_config(r->request_config, &redirectionio_module, ctx);

src/mod_redirectionio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ typedef struct {
5050
} redirectionio_connection;
5151

5252
typedef struct {
53+
struct REDIRECTIONIO_Request *request;
5354
struct REDIRECTIONIO_Action *action;
55+
struct REDIRECTIONIO_HeaderMap *response_headers;
5456
struct REDIRECTIONIO_FilterBodyAction *body_filter;
5557
} redirectionio_context;
5658

src/redirectionio_protocol.c

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
static char errbuf[1024];
99

10-
const char COMMAND_LOG_QUERY[] = "{ \"project_id\": \"%s\", \"request_uri\": \"%s\", \"host\": \"%s\", \"rule_id\": \"%s\", \"target\": \"%s\", \"status_code\": %d, \"user_agent\": \"%s\", \"referer\": \"%s\", \"method\": \"%s\", \"proxy\": \"%s\" }";
11-
1210
static apr_status_t redirectionio_send_uint8(redirectionio_connection *conn, unsigned char uint8);
1311

1412
static apr_status_t redirectionio_send_uint16(redirectionio_connection *conn, apr_uint16_t uint16);
@@ -27,14 +25,13 @@ static apr_status_t redirectionio_action_cleanup(void *action);
2725

2826
static apr_status_t redirectionio_request_cleanup(void *request);
2927

30-
static apr_status_t redirectionio_request_serialized_cleanup(void *request_serialized);
28+
static apr_status_t redirectionio_response_headers_cleanup(void *response_headers);
3129

3230
apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redirectionio_context *ctx, request_rec *r, const char *project_key) {
3331
apr_uint32_t alen;
3432
apr_status_t rv;
35-
struct REDIRECTIONIO_Request *redirectionio_request;
3633
struct REDIRECTIONIO_HeaderMap *first_header = NULL, *current_header = NULL;
37-
const char *request_serialized;
34+
const char *request_serialized, *scheme;
3835
char *action_serialized;
3936
const apr_array_header_t *tarr = apr_table_elts(r->headers_in);
4037
const apr_table_entry_t *telts = (const apr_table_entry_t*)tarr->elts;
@@ -56,23 +53,22 @@ apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redire
5653
}
5754

5855
// Create redirection io request
59-
redirectionio_request = (struct REDIRECTIONIO_Request *)redirectionio_request_create(r->unparsed_uri, r->method, first_header);
56+
scheme = r->parsed_uri.scheme ? r->parsed_uri.scheme : ap_http_scheme(r);
57+
ctx->request = (struct REDIRECTIONIO_Request *)redirectionio_request_create(r->unparsed_uri, r->hostname, scheme, r->method, first_header);
6058

61-
if (redirectionio_request == NULL) {
59+
if (ctx->request == NULL) {
6260
return APR_EGENERAL;
6361
}
6462

65-
apr_pool_pre_cleanup_register(r->pool, redirectionio_request, redirectionio_request_cleanup);
63+
apr_pool_pre_cleanup_register(r->pool, ctx->request, redirectionio_request_cleanup);
6664

6765
// Serialize request
68-
request_serialized = redirectionio_request_json_serialize(redirectionio_request);
66+
request_serialized = redirectionio_request_json_serialize(ctx->request);
6967

7068
if (request_serialized == NULL) {
7169
return APR_EGENERAL;
7270
}
7371

74-
apr_pool_pre_cleanup_register(r->pool, request_serialized, redirectionio_request_serialized_cleanup);
75-
7672
// Send protocol header
7773
rv = redirectionio_send_protocol_header(conn, project_key, REDIRECTIONIO_PROTOCOL_COMMAND_MATCH_ACTION, r);
7874

@@ -84,13 +80,15 @@ apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redire
8480
rv = redirectionio_send_uint32(conn, strlen(request_serialized));
8581

8682
if (rv != APR_SUCCESS) {
83+
free((void *)request_serialized);
8784
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_redirectionio: Error sending request length: %s", apr_strerror(rv, errbuf, sizeof(errbuf)));
8885

8986
return rv;
9087
}
9188

9289
// Send serialized request
9390
rv = redirectionio_send_string(conn, request_serialized, strlen(request_serialized));
91+
free((void *)request_serialized);
9492

9593
if (rv != APR_SUCCESS) {
9694
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_redirectionio: Error sending request: %s", apr_strerror(rv, errbuf, sizeof(errbuf)));
@@ -136,76 +134,29 @@ apr_status_t redirectionio_protocol_match(redirectionio_connection *conn, redire
136134

137135
apr_status_t redirectionio_protocol_log(redirectionio_connection *conn, redirectionio_context *ctx, request_rec *r, const char *project_key) {
138136
apr_size_t wlen;
139-
const char *location;
140-
const char *user_agent = apr_table_get(r->headers_in, "User-Agent");
141-
const char *referer = apr_table_get(r->headers_in, "Referer");
142-
char *rule_id_str = NULL;
143137
apr_status_t rv;
144-
char *dst;
145138
request_rec *response = r;
139+
const char *log;
146140

147141
// Only trust last response for data
148142
while (response->next) {
149143
response = response->next;
150144
}
151145

152-
location = apr_table_get(response->headers_out, "Location");
153-
154-
if (location == NULL) {
155-
location = "";
156-
}
157-
158-
if (user_agent == NULL) {
159-
user_agent = "";
160-
}
161-
162-
if (referer == NULL) {
163-
referer = "";
164-
}
146+
log = redirectionio_api_create_log_in_json(ctx->request, response->status, ctx->response_headers, ctx->action, PROXY_VERSION_STR(PROXY_VERSION), response->request_time);
165147

166-
if (rule_id_str == NULL) {
167-
rule_id_str = "";
148+
if (log == NULL) {
149+
return APR_EGENERAL;
168150
}
169151

170-
wlen =
171-
sizeof(COMMAND_LOG_QUERY)
172-
+ strlen(project_key)
173-
+ strlen(r->unparsed_uri)
174-
+ strlen(r->hostname)
175-
+ strlen(rule_id_str)
176-
+ strlen(location)
177-
+ 3 // Status code length
178-
+ strlen(user_agent)
179-
+ strlen(referer)
180-
+ strlen(r->method)
181-
+ strlen(PROXY_VERSION_STR(PROXY_VERSION))
182-
- 20 // 10 * 2 (%x) characters replaced with values
183-
;
184-
185-
dst = (char *) apr_palloc(r->pool, wlen);
186-
187-
sprintf(
188-
dst,
189-
COMMAND_LOG_QUERY,
190-
project_key,
191-
r->unparsed_uri,
192-
r->hostname,
193-
rule_id_str,
194-
location,
195-
response->status,
196-
user_agent,
197-
referer,
198-
r->method,
199-
PROXY_VERSION_STR(PROXY_VERSION)
200-
);
201-
202152
// Send protocol header
203153
rv = redirectionio_send_protocol_header(conn, project_key, REDIRECTIONIO_PROTOCOL_COMMAND_LOG, r);
204154

205155
if (rv != APR_SUCCESS) {
206156
return rv;
207157
}
208158

159+
wlen = strlen(log);
209160
rv = redirectionio_send_uint32(conn, wlen);
210161

211162
if (rv != APR_SUCCESS) {
@@ -214,7 +165,7 @@ apr_status_t redirectionio_protocol_log(redirectionio_connection *conn, redirect
214165
return rv;
215166
}
216167

217-
rv = apr_socket_send(conn->rio_sock, dst, &wlen);
168+
rv = apr_socket_send(conn->rio_sock, log, &wlen);
218169

219170
if (rv != APR_SUCCESS) {
220171
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_redirectionio: Error sending log command data: %s", apr_strerror(rv, errbuf, sizeof(errbuf)));
@@ -248,12 +199,14 @@ apr_status_t redirectionio_protocol_send_filter_headers(redirectionio_context *c
248199
}
249200

250201
first_header = (struct REDIRECTIONIO_HeaderMap *)redirectionio_action_header_filter_filter(ctx->action, first_header, r->status);
202+
ctx->response_headers = first_header;
251203

252204
// Even if error returns success, as it does not affect anything
253205
if (first_header == NULL) {
254206
return APR_SUCCESS;
255207
}
256208

209+
apr_pool_pre_cleanup_register(r->pool, ctx->response_headers, redirectionio_response_headers_cleanup);
257210
apr_table_clear(r->headers_out);
258211

259212
while (first_header != NULL) {
@@ -262,15 +215,9 @@ apr_status_t redirectionio_protocol_send_filter_headers(redirectionio_context *c
262215
value_str = apr_pstrdup(r->pool, first_header->value);
263216

264217
apr_table_setn(r->headers_out, name_str, value_str);
265-
266-
free((void *)first_header->name);
267-
free((void *)first_header->value);
268218
}
269219

270-
current_header = first_header->next;
271-
free(first_header);
272-
273-
first_header = current_header;
220+
first_header = first_header->next;
274221
}
275222

276223
return APR_SUCCESS;
@@ -454,8 +401,20 @@ static apr_status_t redirectionio_request_cleanup(void *request) {
454401
return APR_SUCCESS;
455402
}
456403

457-
static apr_status_t redirectionio_request_serialized_cleanup(void *request_serialized) {
458-
free(request_serialized);
404+
static apr_status_t redirectionio_response_headers_cleanup(void *response_headers) {
405+
struct REDIRECTIONIO_HeaderMap *first_header, *tmp_header;
406+
407+
first_header = (struct REDIRECTIONIO_HeaderMap *)response_headers;
408+
409+
while (first_header != NULL) {
410+
tmp_header = first_header->next;
411+
412+
free((void *)first_header->name);
413+
free((void *)first_header->value);
414+
free((void *)first_header);
415+
416+
first_header = tmp_header;
417+
}
459418

460419
return APR_SUCCESS;
461420
}

src/redirectionio_protocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "mod_redirectionio.h"
55
#include "httpd.h"
66

7+
const char *ap_run_http_scheme(const request_rec *r);
8+
79
#define REDIRECTIONIO_PROTOCOL_VERSION_MAJOR 1
810
#define REDIRECTIONIO_PROTOCOL_VERSION_MINOR 0
911

0 commit comments

Comments
 (0)