-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-target-handler.c
169 lines (144 loc) · 4.17 KB
/
proxy-target-handler.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <unistd.h>
#include "proxy-handler.h"
#include "proxy-utils.h"
#include "sockets-handler.h"
#include "proxy-target-handler.h"
#define BUFFER_SIZE 4096
/**
* Handles target response initial line.
*/
static int handle_response_message_complete(http_parser* parser) {
target_state_t* state = (target_state_t*)parser->data;
state->message_complete = true;
return 0;
}
static http_parser_settings http_response_callbacks = {
NULL, /* on_message_begin */
NULL, /* on_url */
NULL, /* on_response_status */
NULL, /* on_header_field */
NULL, /* on_header_value */
NULL, /* on_headers_complete */
NULL, /* on_response_body */
handle_response_message_complete,
NULL, /* on_chunk_header */
NULL /* on_chunk_complete */
};
/**
* Handles target input data.
*/
static int target_input_handler(target_state_t* state) {
char buff[BUFFER_SIZE];
ssize_t result;
size_t nparsed;
result = recv(state->socket, buff, BUFFER_SIZE, 0);
if (result == -1) {
if (errno != EAGAIN) {
perror("Cannot recv data from target");
return -1;
}
return 0;
} else if (result == 0)
return 1;
if (state->parser.status_code == 0) {
nparsed = http_parser_execute(&state->parser, &http_response_callbacks,
buff, result);
if (nparsed != result) {
fprintf(stderr, "Cannot parse http input from target socket\n");
return -1;
}
}
if (!cache_entry_append(state->cache, buff, result)) {
fprintf(stderr, "Cannot store target data to cache\n");
return -1;
}
return 0;
}
/**
* Cleanup all target data.
*/
static void target_cleanup(target_state_t* state) {
pthread_mutex_unlock(&state->lock);
sockets_remove_socket(state->socket);
pstring_free(&state->outbuff);
pthread_cond_destroy(&state->notifier);
pthread_mutex_destroy(&state->lock);
free(state);
}
static bool target_init(target_state_t* state) {
int error;
if (!sockets_add_socket(state->socket, &target_handler, state)) {
cache_entry_mark_invalid_and_finished(state->cache);
target_cleanup(state);
return false;
}
sockets_enable_io_handle(state->socket);
error = pthread_mutex_lock(&state->lock);
if (error) {
proxy_error(error, "Cannot lock target lock");
cache_entry_mark_invalid_and_finished(state->cache);
target_cleanup(state);
return false;
}
return true;
}
void* target_thread(void* arg) {
target_state_t* state = (target_state_t*)arg;
int result, events, error;
if (!target_init(state))
return NULL;
while (1) {
error = pthread_cond_wait(&state->notifier, &state->lock);
if (error) {
proxy_error(error, "Cannot wait target condition");
cache_entry_mark_invalid_and_finished(state->cache);
target_cleanup(state);
return NULL;
}
events = state->revents;
// Handle output
if (events & POLLOUT) {
result = send_pstring(state->socket, &state->outbuff);
if (result == -1) {
cache_entry_mark_invalid_and_finished(state->cache);
target_cleanup(state);
return NULL;
} else if (result == 0)
sockets_cancel_out_handle(state->socket);
}
// Handle input
if (events & (POLLIN | POLLPRI)) {
result = target_input_handler(state);
if (result == -1) {
// If parse/receive error, mark invalid and finish
cache_entry_mark_invalid_and_finished(state->cache);
target_cleanup(state);
return NULL;
} else if (result == 1 && !state->message_complete) {
state->message_complete = true;
continue;
}
}
// Handle ending
if (state->message_complete) {
// If not OK code, mark entry as invalid
if (state->parser.status_code != 200)
cache_entry_mark_invalid_and_finished(state->cache);
else
cache_entry_mark_finished(state->cache);
target_cleanup(state);
return NULL;
}
}
}
void target_handler(int socket, int events, void* arg) {
target_state_t* state = (target_state_t*)arg;
state->revents = events;
pthread_cond_signal(&state->notifier);
}