[MINOR] code factoring : capture_headers() serves requests and responses
Both request and response captures will have to parse headers following
the same methods. It's better to factorize the code, hence the new
capture_headers() function.
diff --git a/src/proto_http.c b/src/proto_http.c
index 0eea5fd..f967c52 100644
--- a/src/proto_http.c
+++ b/src/proto_http.c
@@ -516,6 +516,59 @@
} \
} while (0)
+
+/*
+ * Capture headers from message starting at <som> according to header list
+ * <cap_hdr>, and fill the <idx> structure appropriately.
+ */
+void capture_headers(char *som, struct hdr_idx *idx,
+ char **cap, struct cap_hdr *cap_hdr)
+{
+ char *eol, *sol, *col, *sov;
+ int cur_idx;
+ struct cap_hdr *h;
+ int len;
+
+ sol = som + hdr_idx_first_pos(idx);
+ cur_idx = hdr_idx_first_idx(idx);
+
+ while (cur_idx) {
+ eol = sol + idx->v[cur_idx].len;
+
+ col = sol;
+ while (col < eol && *col != ':')
+ col++;
+
+ sov = col + 1;
+ while (sov < eol && http_is_lws[(unsigned char)*sov])
+ sov++;
+
+ for (h = cap_hdr; h; h = h->next) {
+ if ((h->namelen == col - sol) &&
+ (strncasecmp(sol, h->name, h->namelen) == 0)) {
+ if (cap[h->index] == NULL)
+ cap[h->index] =
+ pool_alloc_from(h->pool, h->len + 1);
+
+ if (cap[h->index] == NULL) {
+ Alert("HTTP capture : out of memory.\n");
+ continue;
+ }
+
+ len = eol - sov;
+ if (len > h->len)
+ len = h->len;
+
+ memcpy(cap[h->index], sov, len);
+ cap[h->index][len]=0;
+ }
+ }
+ sol = eol + idx->v[cur_idx].cr + 1;
+ cur_idx = idx->v[cur_idx].next;
+ }
+}
+
+
/*
* This function parses a response line between <ptr> and <end>, starting with
* parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
@@ -1346,50 +1399,9 @@
/* 5: we may need to capture headers */
- if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->fiprm->req_cap)) {
- char *eol, *sol, *col, *sov;
- int cur_idx;
- struct cap_hdr *h;
- int len;
-
- sol = req->data + msg->som + hdr_idx_first_pos(&txn->hdr_idx);
- cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
-
- while (cur_idx) {
- eol = sol + txn->hdr_idx.v[cur_idx].len;
-
- col = sol;
- while (col < eol && *col != ':')
- col++;
-
- sov = col + 1;
- while (sov < eol && http_is_lws[(unsigned char)*sov])
- sov++;
-
- for (h = t->fe->fiprm->req_cap; h; h = h->next) {
- if ((h->namelen == col - sol) &&
- (strncasecmp(sol, h->name, h->namelen) == 0)) {
- if (txn->req.cap[h->index] == NULL)
- txn->req.cap[h->index] =
- pool_alloc_from(h->pool, h->len + 1);
-
- if (txn->req.cap[h->index] == NULL) {
- Alert("HTTP capture : out of memory.\n");
- continue;
- }
-
- len = eol - sov;
- if (len > h->len)
- len = h->len;
-
- memcpy(txn->req.cap[h->index], sov, len);
- txn->req.cap[h->index][len]=0;
- }
- }
- sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
- cur_idx = txn->hdr_idx.v[cur_idx].next;
- }
- }
+ if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->fiprm->req_cap))
+ capture_headers(req->data + msg->som, &txn->hdr_idx,
+ txn->req.cap, t->fe->fiprm->req_cap);
/*
* 6: we will have to evaluate the filters.