Hi

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.

On Fri, Nov 30, 2012 at 03:01:52PM +0100, nr wrote:
> 
> I found out as a pointer to the session_request is included in
> the callback call, I can add the needed mk structures in a custom
> header file and access the http header data in sr->body.data
> 
> It's not pretty, but it do work for my simple needs.
> 
> struct session_request *srp = (struct session_request *) sr;
> printf("Data: \"%s\"\n", srp->body.data);
> 
> I'm planing to look into Duda in the future then I'm in need for
> more advanced functionality like websockets. Does it work as
> embedded library?
> 
> Regards,
> Björn
> 
> On Wed, 28 Nov 2012, Eduardo Silva wrote:
> 
> >On Wed, Nov 28, 2012 at 2:19 AM, nr <[email protected]> wrote:
> >
> >>
> >>If I do want to get the HTTP request hearder while in the callback
> >>function, how do I do that?
> >>
> >>In my program I do set a session cookie with unique session id number that
> >>is included in every client request as a single line in the HTTP header.
> >>
> >>Regards,
> >>Bjön
> >>_______________________________________________
> >>Monkey mailing list
> >>[email protected]
> >>http://lists.monkey-project.com/listinfo/monkey
> >>
> >>
> >that is no supported yet in the Library version, if you are playing with
> >cookies and sessions i recommend you to take a look to the Duda framework:
> >
> > http://duda.io
> >
> >there you have a few C pseudo-objects to handle sessions, cookies and
> >customize response headers:
> >
> > http://duda.io/api/session.html
> >
> >if you want come to irc.freenode.net #monkey so we can give you further
> >assistance,
> >
> >regards,
> >
> >-- 
> >Eduardo Silva
> >http://edsiper.linuxchile.cl
> >http://www.monkey-project.com
> >

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


-- 
Sonny Karlsson
>From 5472b8d3abeb184806d5b7dc26e766e9500fa0ae 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 get_request_header.


Signed-off-by: Sonny Karlsson <[email protected]>
---
 src/include/public/monkey.h |   13 ++++++++++
 src/mk_lib.c                |   55 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/src/include/public/monkey.h b/src/include/public/monkey.h
index 8f870ba..bad03f2 100644
--- a/src/include/public/monkey.h
+++ b/src/include/public/monkey.h
@@ -180,6 +180,19 @@ 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 the value of a http header.
+ *
+ * The return value is 0 on successful execution else -1.
+ *
+ * If a key is found and it has a value set, the data will be duplicated
+ * and the value argument will be pointed at it.
+ * If the key isn't fond or the value is a zero length string the value
+ * argument will point at a NULL pointer.
+ *
+ * Please free *value when it isn't needed anymore.
+ */
+int mklib_get_request_header(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..1f0761f 100644
--- a/src/mk_lib.c
+++ b/src/mk_lib.c
@@ -621,4 +621,59 @@ int mklib_mimetype_add(mklib_ctx ctx, const char *name, 
const char *type)
     return MKLIB_TRUE;
 }
 
+int mklib_get_request_header(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_get_request_header: mklib_session is not set.");
+      return -1;
+   }
+
+   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 0;
+      }
+
+      *value = mk_mem_malloc_z(value_len + 1);
+      if (*value == NULL) {
+         mk_err("mklib_get_request_header: Malloc failed.");
+         return -1;
+      }
+      memcpy(*value, row[i].init + key_len + 2, value_len);
+      return 0;
+   }
+
+   *value = NULL;
+   return 0;
+}
+
 #endif
-- 
1.7.10.4

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

Reply via email to