Skip to content
Permalink
Browse files Browse the repository at this point in the history
Remote addr (#99)
* REMOTE_ADDR added

* Ready for merge
  • Loading branch information
ohler55 committed Nov 8, 2020
1 parent 7345b0c commit 23d0353
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 46 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,16 @@

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

## [2.14.0] - 2020-11-07

### Added

- REMOTE_ADDR element added to requests/env argument to `call()`.

- Added check for multiple Content-Length headers.

- Multiple occurrances of a header are now passed to the Rack `call()` method as an array.

## [2.13.0] - 2020-07-05

### Added
Expand Down
17 changes: 17 additions & 0 deletions ext/agoo/con.c
Expand Up @@ -63,6 +63,22 @@ agoo_con_create(agooErr err, int sock, uint64_t id, agooBind b) {
if (NULL == (c = (agooCon)AGOO_CALLOC(1, sizeof(struct _agooCon)))) {
AGOO_ERR_MEM(err, "Connection");
} else {
// It would be better to get this information in server.c after
// accept() but that does not work on macOS so instead a call to
// getpeername() is used instead.
struct sockaddr_storage addr;
socklen_t len = sizeof(addr);

getpeername(sock, (struct sockaddr*)&addr, &len);
if (addr.ss_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in*)&addr;

inet_ntop(AF_INET, &s->sin_addr, c->remote, sizeof(c->remote));
} else {
struct sockaddr_in6 *s = (struct sockaddr_in6*)&addr;

inet_ntop(AF_INET6, &s->sin6_addr, c->remote, sizeof(c->remote));
}
c->sock = sock;
c->id = id;
c->timeout = dtime() + CON_TIMEOUT;
Expand Down Expand Up @@ -437,6 +453,7 @@ con_header_read(agooCon c, size_t *mlenp) {
c->req->method = method;
c->req->upgrade = AGOO_UP_NONE;
c->req->up = NULL;
memcpy(c->req->remote, c->remote, sizeof(c->remote));
c->req->path.start = c->req->msg + (path.start - c->buf);
c->req->path.len = (int)(path.end - path.start);
c->req->query.start = c->req->msg + (query - c->buf);
Expand Down
2 changes: 2 additions & 0 deletions ext/agoo/con.h
Expand Up @@ -3,6 +3,7 @@
#ifndef AGOO_CON_H
#define AGOO_CON_H

#include <arpa/inet.h>
#include <poll.h>
#include <pthread.h>
#include <stdbool.h>
Expand Down Expand Up @@ -45,6 +46,7 @@ typedef struct _agooCon {
struct _agooBind *bind;
struct pollfd *pp;
uint64_t id;
char remote[INET6_ADDRSTRLEN];
char buf[MAX_HEADER_SIZE];
size_t bcnt;

Expand Down
55 changes: 20 additions & 35 deletions ext/agoo/http.c
Expand Up @@ -26,7 +26,7 @@ typedef struct _cache {
struct _cache key_cache;

// The rack spec indicates the characters (),/:;<=>?@[]{} are invalid which
// clearly is not consisten with RFC7230 so stick with the RFC.
// clearly is not consistent with RFC7230 so stick with the RFC.
static char header_value_chars[256] = "\
xxxxxxxxxxoxxxxxxxxxxxxxxxxxxxxx\
oooooooooooooooooooooooooooooooo\
Expand All @@ -50,7 +50,6 @@ static const char *header_keys[] = {
"Accept-Encoding",
"Accept-Features",
"Accept-Language",
"Accept-Language",
"Accept-Patch",
"Accept-Post",
"Accept-Ranges",
Expand All @@ -74,7 +73,6 @@ static const char *header_keys[] = {
"Approved",
"Archive",
"Archived-At",
"Archived-At",
"Article-Names",
"Article-Updates",
"Authentication-Control",
Expand All @@ -99,36 +97,27 @@ static const char *header_keys[] = {
"Cc",
"Close",
"Comments",
"Comments",
"Compliance",
"Connection",
"Content-Alternative",
"Content-Base",
"Content-Base",
"Content-Description",
"Content-Disposition",
"Content-Disposition",
"Content-Duration",
"Content-Encoding",
"Content-ID",
"Content-ID",
"Content-Identifier",
"Content-Language",
"Content-Language",
"Content-Length",
"Content-Location",
"Content-Location",
"Content-MD5",
"Content-MD5",
"Content-Range",
"Content-Return",
"Content-Script-Type",
"Content-Style-Type",
"Content-Transfer-Encoding",
"Content-Transfer-Encoding",
"Content-Translation-Type",
"Content-Type",
"Content-Type",
"Content-Version",
"Content-features",
"Control",
Expand All @@ -141,8 +130,7 @@ static const char *header_keys[] = {
"DAV",
"DKIM-Signature",
"DL-Expansion-History",
"Date",
"Date",
"DNT",
"Date",
"Date-Received",
"Default-Style",
Expand Down Expand Up @@ -182,24 +170,20 @@ static const char *header_keys[] = {
"Downgraded-Sender",
"Downgraded-To",
"EDIINT-Features",
"EDIINT-Features",
"ETag",
"Eesst-Version",
"Encoding",
"Encrypted",
"Errors-To",
"Expect",
"Expires",
"Expires",
"Expires",
"Expiry-Date",
"Ext",
"Followup-To",
"Form-Sub",
"Forwarded",
"From",
"From",
"From",
"Front-End-Https",
"Generate-Delivery-Report",
"GetProfile",
"HTTP2-Settings",
Expand All @@ -219,10 +203,8 @@ static const char *header_keys[] = {
"Injection-Date",
"Injection-Info",
"Jabber-ID",
"Jabber-ID",
"Keep-Alive",
"Keywords",
"Keywords",
"Label",
"Language",
"Last-Modified",
Expand All @@ -240,7 +222,6 @@ static const char *header_keys[] = {
"Location",
"Lock-Token",
"MIME-Version",
"MIME-Version",
"MMHS-Acp127-Message-Identifier",
"MMHS-Authorizing-Users",
"MMHS-Codress-Message-Indicator",
Expand All @@ -262,8 +243,6 @@ static const char *header_keys[] = {
"Memento-Datetime",
"Message-Context",
"Message-ID",
"Message-ID",
"Message-ID",
"Message-Type",
"Meter",
"Method-Check",
Expand All @@ -279,7 +258,6 @@ static const char *header_keys[] = {
"Optional-WWW-Authenticate",
"Ordering-Type",
"Organization",
"Organization",
"Origin",
"Original-Encoded-Information-Types",
"Original-From",
Expand All @@ -292,7 +270,6 @@ static const char *header_keys[] = {
"P3P",
"PEP",
"PICS-Label",
"PICS-Label",
"Path",
"Pep-Info",
"Position",
Expand All @@ -311,6 +288,7 @@ static const char *header_keys[] = {
"Proxy-Authenticate",
"Proxy-Authentication-Info",
"Proxy-Authorization",
"Proxy-Connection",
"Proxy-Features",
"Proxy-Instruction",
"Public",
Expand All @@ -321,13 +299,11 @@ static const char *header_keys[] = {
"Received-SPF",
"Redirect-Ref",
"References",
"References",
"Referer",
"Referer-Root",
"Relay-Version",
"Reply-By",
"Reply-To",
"Reply-To",
"Require-Recipient-Valid-Since",
"Resent-Bcc",
"Resent-Cc",
Expand All @@ -345,6 +321,7 @@ static const char *header_keys[] = {
"SIO-Label-History",
"SLUG",
"Safe",
"Save-Data",
"Schedule-Reply",
"Schedule-Tag",
"Sec-WebSocket-Accept",
Expand All @@ -367,11 +344,9 @@ static const char *header_keys[] = {
"Strict-Transport-Security",
"SubOK",
"Subject",
"Subject",
"Subst",
"Summary",
"Supersedes",
"Supersedes",
"Surrogate-Capability",
"Surrogate-Control",
"TCN",
Expand All @@ -390,9 +365,9 @@ static const char *header_keys[] = {
"UA-Windowpixels",
"URI",
"Upgrade",
"Upgrade-Insecure-Requests",
"Urgency",
"User-Agent",
"User-Agent",
"VBR-Info",
"Variant-Vary",
"Vary",
Expand All @@ -401,22 +376,32 @@ static const char *header_keys[] = {
"WWW-Authenticate",
"Want-Digest",
"Warning",
"X-Archived-At",
"X-ATT-DeviceId",
"X-Archived-At",
"X-Content-Type-Options",
"X-Correlation-ID",
"X-Csrf-Token",
"X-Device-Accept",
"X-Device-Accept-Charset",
"X-Device-Accept-Encoding",
"X-Device-Accept-Language",
"X-Device-User-Agent",
"X-Forwarded-For",
"X-Forwarded-Host",
"X-Forwarded-Proto",
"X-Frame-Options",
"X-Http-Method-Override",
"X-Mittente",
"X-PGP-Sig",
"X-Request-ID",
"X-Requested-With",
"X-Ricevuta",
"X-Riferimento-Message-ID",
"X-TipoRicevuta",
"X-Trasporto",
"X-UIDH",
"X-VerificaSicurezza",
"X-Wap-Profile",
"X-XSS-Protection",
"X400-Content-Identifier",
"X400-Content-Return",
Expand Down Expand Up @@ -469,7 +454,7 @@ key_set(const char *key) {
int64_t h = calc_hash(key, &len);
Slot *bucket = get_bucketp(h);
Slot s;

if (NULL != (s = (Slot)AGOO_MALLOC(sizeof(struct _slot)))) {
s->hash = h;
s->klen = len;
Expand All @@ -482,7 +467,7 @@ key_set(const char *key) {
void
agoo_http_init() {
const char **kp = header_keys;

memset(&key_cache, 0, sizeof(struct _cache));
for (; NULL != *kp; kp++) {
key_set(*kp);
Expand Down Expand Up @@ -550,7 +535,7 @@ agoo_http_header_ok(agooErr err, const char *key, int klen, const char *value, i
const char*
agoo_http_code_message(int code) {
const char *msg = "";

switch (code) {
case 100: msg = "Continue"; break;
case 101: msg = "Switching Protocols"; break;
Expand Down
2 changes: 2 additions & 0 deletions ext/agoo/req.h
Expand Up @@ -3,6 +3,7 @@
#ifndef AGOO_REQ_H
#define AGOO_REQ_H

#include <arpa/inet.h>
#include <stdint.h>

#include "hook.h"
Expand Down Expand Up @@ -32,6 +33,7 @@ typedef struct _agooReq {
struct _agooStr query;
struct _agooStr header;
struct _agooStr body;
char remote[INET6_ADDRSTRLEN];
void *env;
agooHook hook;
size_t mlen; // allocated msg length
Expand Down

0 comments on commit 23d0353

Please sign in to comment.