On Sat, Dec 01, 2012 at 11:06:40AM +0200, Lauri Kasanen wrote:
> On Fri, 30 Nov 2012 20:50:54 +0100
> Sonny Karlsson <[email protected]> wrote:
> 
> > I'm answering a bit late, but I definitely think this functionality
> > should be part of the monkey lib so I've created a patch. This patch
> > adds a function for fetching a request header (mklib_get_request_header)
> > inside a cb_data or cb_close (untested) callback. The function solves
> > this problem almost the same way, but using the headers_toc (headers
> > table of content) instead.
> 
> Hi
> 
> Certainly a good function to add.
> 
> Ed, I wish you'd have waited a day or two for me to have time to review this 
> ;)
> Please revert the current version.
> 
> @Sonny:
> 
> - please use the MKLIB_TRUE, MKLIB_FALSE return values for consistency

Done. Also renamed the function mk_request_header_get as most other
names follow this pattern.

> - on a cursory look, this looks like it's duplicated code from elsewhere - 
> I'm not sure. Is it?

There is similar code in mk_request.c, but it toggles a 'status' flag on
so entries they won't be read twice. I can see numerous problems with
that in a library setting. Monkey itself access some of the headers
when populating session_request/client_session structs, which are not
public. The mk_request.c version is also exposed in the plugin api,
possibly hiding even more headers. To access all headers I must write it
like this.

> - all mklib functions are documented. Please submit a man page for the new 
> function

Added txt source. Also listed in the SEE ALSO section of
mklib_callback_set(3).

> - the new function wasn't exported to the library (MK_EXPORT), so it won't be 
> accessible to users as is

Sorry about this, it disappeared in a git stash. I've tested this patch
on a clean branch, as is my usual routine.

> 
> If you don't have the required asciidoc setup, submitting the text source is 
> enough, I can generate the man page. (under man/asc-src/).

I'd be grateful if you could generate it, the asciidoc installation
requires 1.2 GB and I'd rather use the space on my raspberry pi for
something else.

> 
> - Lauri
> _______________________________________________
> Monkey mailing list
> [email protected]
> http://lists.monkey-project.com/listinfo/monkey

-- 
Sonny Karlsson
>From 6e44d5ef2b82d7a97cfa002bd5af2d038f3b336e Mon Sep 17 00:00:00 2001
From: Sonny Karlsson <[email protected]>
Date: Fri, 30 Nov 2012 20:11:44 +0100
Subject: [PATCH] lib: Add mklib_request_header_get.

Signed-off-by: Sonny Karlsson <[email protected]>
---
 man/asc-src/mklib_callback_set.txt       |    2 +-
 man/asc-src/mklib_request_header_get.txt |   45 +++++++++++++++++++++
 src/include/public/monkey.h              |    3 ++
 src/mk_lib.c                             |   63 ++++++++++++++++++++++++++++++
 4 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 man/asc-src/mklib_request_header_get.txt

diff --git a/man/asc-src/mklib_callback_set.txt 
b/man/asc-src/mklib_callback_set.txt
index 6d0e303..4344e70 100644
--- a/man/asc-src/mklib_callback_set.txt
+++ b/man/asc-src/mklib_callback_set.txt
@@ -87,7 +87,7 @@ SEE ALSO
 --------
 *mklib_config*(3), *mklib_vhost_config*(3), *mklib_start*(3), *mklib_stop*(3), 
 *mklib_init*(3), *mklib_vhost_list*(3), *mklib_scheduler_worker_info*(3),
-*mklib_mimetype_add*(3)
+*mklib_mimetype_add*(3), *mklib_request_header_get*(3)
 
 RESOURCES
 ---------
diff --git a/man/asc-src/mklib_request_header_get.txt 
b/man/asc-src/mklib_request_header_get.txt
new file mode 100644
index 0000000..2bc0a80
--- /dev/null
+++ b/man/asc-src/mklib_request_header_get.txt
@@ -0,0 +1,45 @@
+mklib_request_header_get(3)
+===========================
+
+Name
+----
+mklib_request_header_get - get header from request
+
+SYNOPSIS
+--------
+*#include <monkey.h>*
+
+[verse]
+*int mklib_request_header_get(const mklib_session *'session',
+       const char *'key',
+       char `**`'value');*
+
+DESCRIPTION
+-----------
+The *mklib_request_header_get*() function finds a header value.
+
+The 'session' argument is the current request session. 'session' may
+not be NULL.
+
+The 'key' argument is the header to search for. The full name of the
+header must be provided but it is not required to be match case.
+'key' may not be NULL or point at an empty string.
+
+The char* address pointed at by the 'value' argument is used to return
+an allocated string with the value. If no value is found, a NULL value
+is returned. 'value' may not be NULL.
+
+This function is expected to be used inside of the 'cb_data' or
+'cb_close' callback functions.
+
+RETURN VALUE
+------------
+On success MKLIB_TRUE is returned, on failure MKLIB_FALSE.
+
+SEE ALSO
+--------
+*mklib_callback_set*(3)
+
+RESOURCES
+---------
+See http://monkey-project.com
diff --git a/src/include/public/monkey.h b/src/include/public/monkey.h
index 8f870ba..284b065 100644
--- a/src/include/public/monkey.h
+++ b/src/include/public/monkey.h
@@ -180,6 +180,9 @@ struct mklib_mime MK_EXPORT 
**mklib_mimetype_list(mklib_ctx);
 /* Add a new mimetype */
 int MK_EXPORT mklib_mimetype_add(mklib_ctx, const char *, const char *);
 
+/* Get value of http header */
+int MK_EXPORT mklib_request_header_get(const mklib_session *ms, const char 
*key, char **value);
+
 #define mklib_vhost_foreach(cur, list) for(cur = *list++; cur; cur = *list++)
 #define mklib_worker_info_foreach(cur, list) mklib_vhost_foreach(cur, list)
 #define mklib_mimetype_foreach(cur, list) mklib_vhost_foreach(cur, list)
diff --git a/src/mk_lib.c b/src/mk_lib.c
index 274fc73..939fe3c 100644
--- a/src/mk_lib.c
+++ b/src/mk_lib.c
@@ -621,4 +621,67 @@ int mklib_mimetype_add(mklib_ctx ctx, const char *name, 
const char *type)
     return MKLIB_TRUE;
 }
 
+int mklib_request_header_get(const mklib_session *ms,
+      const char *key,
+      char **value)
+{
+   const struct session_request *sr = ms;
+   int i, n;
+   size_t len, key_len = strlen(key), value_len;
+   const struct header_toc_row *row;
+
+   if (!sr) {
+      mk_err("mklib_request_header_get: mklib_session is not set.");
+      return MKLIB_FALSE;
+   }
+   if (!key || key[0] == '\0') {
+      mk_err("mklib_request_header_get: No key provided.");
+      return MKLIB_FALSE;
+   }
+   if (!value) {
+      mk_err("mklib_request_header_get: No value ptr provided.");
+      return MKLIB_FALSE;
+   }
+
+   n = sr->headers_toc.length;
+   row = sr->headers_toc.rows;
+
+   for (i = 0; i < n; i++) {
+      if (!row[i].end || !row[i].init) {
+         continue;
+      }
+      if (row[i].end <= row[i].init) {
+         continue;
+      }
+      len = row[i].end - row[i].init;
+      // Expect at least 2 extra chars after key. "<key>: "
+      if (len < key_len + 2 || !!strncasecmp(key, row[i].init, key_len)) {
+         continue;
+      }
+
+      if (row[i].init[key_len] != ':') {
+         // Partial match, skip
+         continue;
+      }
+
+      value_len = len - (key_len + 2);
+      if (value_len == 0) {
+         // Value set to ""
+         *value = NULL;
+         return MKLIB_TRUE;
+      }
+
+      *value = mk_mem_malloc_z(value_len + 1);
+      if (*value == NULL) {
+         mk_err("mklib_request_header_get: Malloc failed.");
+         return MKLIB_FALSE;
+      }
+      memcpy(*value, row[i].init + key_len + 2, value_len);
+      return MKLIB_TRUE;
+   }
+
+   *value = NULL;
+   return MKLIB_TRUE;
+}
+
 #endif
-- 
1.7.10.4

_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to