kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 4a8d8ab7f85c0c5a695eff69afdde3fbc99c3b8f
parent 6dca7d51e6317e0d4a58e1477f99a66f8c0a8d3f
Author: Joris Vink <joris@coders.se>
Date:   Fri, 29 Jun 2018 22:37:48 +0200

log referer in accesslog if present.

Diffstat:
include/kore/http.h | 2++
src/accesslog.c | 16++++++++++++++--
src/http.c | 4++++
3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/include/kore/http.h b/include/kore/http.h @@ -35,6 +35,7 @@ extern "C" { #define HTTP_BODY_MAX_LEN 1024000 #define HTTP_URI_LEN 2000 #define HTTP_USERAGENT_LEN 256 +#define HTTP_REFERER_LEN 256 #define HTTP_REQ_HEADER_MAX 25 #define HTTP_MAX_QUERY_ARGS 20 #define HTTP_MAX_COOKIES 10 @@ -219,6 +220,7 @@ struct http_request { const char *path; const char *host; const char *agent; + const char *referer; struct connection *owner; u_int8_t *headers; struct kore_buf *http_body; diff --git a/src/accesslog.c b/src/accesslog.c @@ -31,6 +31,7 @@ struct kore_log_packet { char host[KORE_DOMAINNAME_LEN]; char path[HTTP_URI_LEN]; char agent[HTTP_USERAGENT_LEN]; + char referer[HTTP_REFERER_LEN]; #if !defined(KORE_NO_TLS) char cn[X509_CN_LENGTH]; #endif @@ -110,9 +111,9 @@ kore_accesslog_write(const void *data, u_int32_t len) (void)strftime(tbuf, sizeof(tbuf), "%d/%b/%Y:%H:%M:%S %z", tm); l = asprintf(&buf, - "%s - %s [%s] \"%s %s HTTP/1.1\" %d %zu \"-\" \"%s\"\n", + "%s - %s [%s] \"%s %s HTTP/1.1\" %d %zu \"%s\" \"%s\"\n", addr, cn, tbuf, method, logpacket.path, logpacket.status, - logpacket.length, logpacket.agent); + logpacket.length, logpacket.referer, logpacket.agent); if (l == -1) { kore_log(LOG_WARNING, "kore_accesslog_write(): asprintf(): %s", errno_s); @@ -171,6 +172,17 @@ kore_accesslog(struct http_request *req) sizeof(logpacket.agent)); } + if (req->referer != NULL) { + if (kore_strlcpy(logpacket.referer, req->referer, + sizeof(logpacket.referer)) >= sizeof(logpacket.referer)) { + kore_log(LOG_NOTICE, + "kore_accesslog: referer truncated"); + } + } else { + (void)kore_strlcpy(logpacket.referer, "-", + sizeof(logpacket.referer)); + } + #if !defined(KORE_NO_TLS) memset(logpacket.cn, '\0', sizeof(logpacket.cn)); if (req->owner->cert != NULL) { diff --git a/src/http.c b/src/http.c @@ -685,6 +685,10 @@ http_header_recv(struct netbuf *nb) if (req->agent == NULL && !strcasecmp(hdr->header, "user-agent")) req->agent = hdr->value; + + if (req->referer == NULL && + !strcasecmp(hdr->header, "referer")) + req->referer = hdr->value; } if (req->flags & HTTP_REQUEST_EXPECT_BODY) {