Skip to content

Commit 23d0353

Browse files
authored
Remote addr (#99)
* REMOTE_ADDR added * Ready for merge
1 parent 7345b0c commit 23d0353

File tree

11 files changed

+113
-46
lines changed

11 files changed

+113
-46
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All changes to the Agoo gem are documented here. Releases follow semantic versioning.
44

5+
## [2.14.0] - 2020-11-07
6+
7+
### Added
8+
9+
- REMOTE_ADDR element added to requests/env argument to `call()`.
10+
11+
- Added check for multiple Content-Length headers.
12+
13+
- Multiple occurrances of a header are now passed to the Rack `call()` method as an array.
14+
515
## [2.13.0] - 2020-07-05
616

717
### Added

ext/agoo/con.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ agoo_con_create(agooErr err, int sock, uint64_t id, agooBind b) {
6363
if (NULL == (c = (agooCon)AGOO_CALLOC(1, sizeof(struct _agooCon)))) {
6464
AGOO_ERR_MEM(err, "Connection");
6565
} else {
66+
// It would be better to get this information in server.c after
67+
// accept() but that does not work on macOS so instead a call to
68+
// getpeername() is used instead.
69+
struct sockaddr_storage addr;
70+
socklen_t len = sizeof(addr);
71+
72+
getpeername(sock, (struct sockaddr*)&addr, &len);
73+
if (addr.ss_family == AF_INET) {
74+
struct sockaddr_in *s = (struct sockaddr_in*)&addr;
75+
76+
inet_ntop(AF_INET, &s->sin_addr, c->remote, sizeof(c->remote));
77+
} else {
78+
struct sockaddr_in6 *s = (struct sockaddr_in6*)&addr;
79+
80+
inet_ntop(AF_INET6, &s->sin6_addr, c->remote, sizeof(c->remote));
81+
}
6682
c->sock = sock;
6783
c->id = id;
6884
c->timeout = dtime() + CON_TIMEOUT;
@@ -437,6 +453,7 @@ con_header_read(agooCon c, size_t *mlenp) {
437453
c->req->method = method;
438454
c->req->upgrade = AGOO_UP_NONE;
439455
c->req->up = NULL;
456+
memcpy(c->req->remote, c->remote, sizeof(c->remote));
440457
c->req->path.start = c->req->msg + (path.start - c->buf);
441458
c->req->path.len = (int)(path.end - path.start);
442459
c->req->query.start = c->req->msg + (query - c->buf);

ext/agoo/con.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef AGOO_CON_H
44
#define AGOO_CON_H
55

6+
#include <arpa/inet.h>
67
#include <poll.h>
78
#include <pthread.h>
89
#include <stdbool.h>
@@ -45,6 +46,7 @@ typedef struct _agooCon {
4546
struct _agooBind *bind;
4647
struct pollfd *pp;
4748
uint64_t id;
49+
char remote[INET6_ADDRSTRLEN];
4850
char buf[MAX_HEADER_SIZE];
4951
size_t bcnt;
5052

ext/agoo/http.c

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ typedef struct _cache {
2626
struct _cache key_cache;
2727

2828
// The rack spec indicates the characters (),/:;<=>?@[]{} are invalid which
29-
// clearly is not consisten with RFC7230 so stick with the RFC.
29+
// clearly is not consistent with RFC7230 so stick with the RFC.
3030
static char header_value_chars[256] = "\
3131
xxxxxxxxxxoxxxxxxxxxxxxxxxxxxxxx\
3232
oooooooooooooooooooooooooooooooo\
@@ -50,7 +50,6 @@ static const char *header_keys[] = {
5050
"Accept-Encoding",
5151
"Accept-Features",
5252
"Accept-Language",
53-
"Accept-Language",
5453
"Accept-Patch",
5554
"Accept-Post",
5655
"Accept-Ranges",
@@ -74,7 +73,6 @@ static const char *header_keys[] = {
7473
"Approved",
7574
"Archive",
7675
"Archived-At",
77-
"Archived-At",
7876
"Article-Names",
7977
"Article-Updates",
8078
"Authentication-Control",
@@ -99,36 +97,27 @@ static const char *header_keys[] = {
9997
"Cc",
10098
"Close",
10199
"Comments",
102-
"Comments",
103100
"Compliance",
104101
"Connection",
105102
"Content-Alternative",
106103
"Content-Base",
107-
"Content-Base",
108104
"Content-Description",
109105
"Content-Disposition",
110-
"Content-Disposition",
111106
"Content-Duration",
112107
"Content-Encoding",
113108
"Content-ID",
114-
"Content-ID",
115109
"Content-Identifier",
116110
"Content-Language",
117-
"Content-Language",
118111
"Content-Length",
119112
"Content-Location",
120-
"Content-Location",
121-
"Content-MD5",
122113
"Content-MD5",
123114
"Content-Range",
124115
"Content-Return",
125116
"Content-Script-Type",
126117
"Content-Style-Type",
127118
"Content-Transfer-Encoding",
128-
"Content-Transfer-Encoding",
129119
"Content-Translation-Type",
130120
"Content-Type",
131-
"Content-Type",
132121
"Content-Version",
133122
"Content-features",
134123
"Control",
@@ -141,8 +130,7 @@ static const char *header_keys[] = {
141130
"DAV",
142131
"DKIM-Signature",
143132
"DL-Expansion-History",
144-
"Date",
145-
"Date",
133+
"DNT",
146134
"Date",
147135
"Date-Received",
148136
"Default-Style",
@@ -182,24 +170,20 @@ static const char *header_keys[] = {
182170
"Downgraded-Sender",
183171
"Downgraded-To",
184172
"EDIINT-Features",
185-
"EDIINT-Features",
186173
"ETag",
187174
"Eesst-Version",
188175
"Encoding",
189176
"Encrypted",
190177
"Errors-To",
191178
"Expect",
192179
"Expires",
193-
"Expires",
194-
"Expires",
195180
"Expiry-Date",
196181
"Ext",
197182
"Followup-To",
198183
"Form-Sub",
199184
"Forwarded",
200185
"From",
201-
"From",
202-
"From",
186+
"Front-End-Https",
203187
"Generate-Delivery-Report",
204188
"GetProfile",
205189
"HTTP2-Settings",
@@ -219,10 +203,8 @@ static const char *header_keys[] = {
219203
"Injection-Date",
220204
"Injection-Info",
221205
"Jabber-ID",
222-
"Jabber-ID",
223206
"Keep-Alive",
224207
"Keywords",
225-
"Keywords",
226208
"Label",
227209
"Language",
228210
"Last-Modified",
@@ -240,7 +222,6 @@ static const char *header_keys[] = {
240222
"Location",
241223
"Lock-Token",
242224
"MIME-Version",
243-
"MIME-Version",
244225
"MMHS-Acp127-Message-Identifier",
245226
"MMHS-Authorizing-Users",
246227
"MMHS-Codress-Message-Indicator",
@@ -262,8 +243,6 @@ static const char *header_keys[] = {
262243
"Memento-Datetime",
263244
"Message-Context",
264245
"Message-ID",
265-
"Message-ID",
266-
"Message-ID",
267246
"Message-Type",
268247
"Meter",
269248
"Method-Check",
@@ -279,7 +258,6 @@ static const char *header_keys[] = {
279258
"Optional-WWW-Authenticate",
280259
"Ordering-Type",
281260
"Organization",
282-
"Organization",
283261
"Origin",
284262
"Original-Encoded-Information-Types",
285263
"Original-From",
@@ -292,7 +270,6 @@ static const char *header_keys[] = {
292270
"P3P",
293271
"PEP",
294272
"PICS-Label",
295-
"PICS-Label",
296273
"Path",
297274
"Pep-Info",
298275
"Position",
@@ -311,6 +288,7 @@ static const char *header_keys[] = {
311288
"Proxy-Authenticate",
312289
"Proxy-Authentication-Info",
313290
"Proxy-Authorization",
291+
"Proxy-Connection",
314292
"Proxy-Features",
315293
"Proxy-Instruction",
316294
"Public",
@@ -321,13 +299,11 @@ static const char *header_keys[] = {
321299
"Received-SPF",
322300
"Redirect-Ref",
323301
"References",
324-
"References",
325302
"Referer",
326303
"Referer-Root",
327304
"Relay-Version",
328305
"Reply-By",
329306
"Reply-To",
330-
"Reply-To",
331307
"Require-Recipient-Valid-Since",
332308
"Resent-Bcc",
333309
"Resent-Cc",
@@ -345,6 +321,7 @@ static const char *header_keys[] = {
345321
"SIO-Label-History",
346322
"SLUG",
347323
"Safe",
324+
"Save-Data",
348325
"Schedule-Reply",
349326
"Schedule-Tag",
350327
"Sec-WebSocket-Accept",
@@ -367,11 +344,9 @@ static const char *header_keys[] = {
367344
"Strict-Transport-Security",
368345
"SubOK",
369346
"Subject",
370-
"Subject",
371347
"Subst",
372348
"Summary",
373349
"Supersedes",
374-
"Supersedes",
375350
"Surrogate-Capability",
376351
"Surrogate-Control",
377352
"TCN",
@@ -390,9 +365,9 @@ static const char *header_keys[] = {
390365
"UA-Windowpixels",
391366
"URI",
392367
"Upgrade",
368+
"Upgrade-Insecure-Requests",
393369
"Urgency",
394370
"User-Agent",
395-
"User-Agent",
396371
"VBR-Info",
397372
"Variant-Vary",
398373
"Vary",
@@ -401,22 +376,32 @@ static const char *header_keys[] = {
401376
"WWW-Authenticate",
402377
"Want-Digest",
403378
"Warning",
404-
"X-Archived-At",
379+
"X-ATT-DeviceId",
405380
"X-Archived-At",
406381
"X-Content-Type-Options",
382+
"X-Correlation-ID",
383+
"X-Csrf-Token",
407384
"X-Device-Accept",
408385
"X-Device-Accept-Charset",
409386
"X-Device-Accept-Encoding",
410387
"X-Device-Accept-Language",
411388
"X-Device-User-Agent",
389+
"X-Forwarded-For",
390+
"X-Forwarded-Host",
391+
"X-Forwarded-Proto",
412392
"X-Frame-Options",
393+
"X-Http-Method-Override",
413394
"X-Mittente",
414395
"X-PGP-Sig",
396+
"X-Request-ID",
397+
"X-Requested-With",
415398
"X-Ricevuta",
416399
"X-Riferimento-Message-ID",
417400
"X-TipoRicevuta",
418401
"X-Trasporto",
402+
"X-UIDH",
419403
"X-VerificaSicurezza",
404+
"X-Wap-Profile",
420405
"X-XSS-Protection",
421406
"X400-Content-Identifier",
422407
"X400-Content-Return",
@@ -469,7 +454,7 @@ key_set(const char *key) {
469454
int64_t h = calc_hash(key, &len);
470455
Slot *bucket = get_bucketp(h);
471456
Slot s;
472-
457+
473458
if (NULL != (s = (Slot)AGOO_MALLOC(sizeof(struct _slot)))) {
474459
s->hash = h;
475460
s->klen = len;
@@ -482,7 +467,7 @@ key_set(const char *key) {
482467
void
483468
agoo_http_init() {
484469
const char **kp = header_keys;
485-
470+
486471
memset(&key_cache, 0, sizeof(struct _cache));
487472
for (; NULL != *kp; kp++) {
488473
key_set(*kp);
@@ -550,7 +535,7 @@ agoo_http_header_ok(agooErr err, const char *key, int klen, const char *value, i
550535
const char*
551536
agoo_http_code_message(int code) {
552537
const char *msg = "";
553-
538+
554539
switch (code) {
555540
case 100: msg = "Continue"; break;
556541
case 101: msg = "Switching Protocols"; break;

ext/agoo/req.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef AGOO_REQ_H
44
#define AGOO_REQ_H
55

6+
#include <arpa/inet.h>
67
#include <stdint.h>
78

89
#include "hook.h"
@@ -32,6 +33,7 @@ typedef struct _agooReq {
3233
struct _agooStr query;
3334
struct _agooStr header;
3435
struct _agooStr body;
36+
char remote[INET6_ADDRSTRLEN];
3537
void *env;
3638
agooHook hook;
3739
size_t mlen; // allocated msg length

0 commit comments

Comments
 (0)