updated ucx

Fri, 25 May 2012 18:16:24 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 25 May 2012 18:16:24 +0200
changeset 31
280250e45ba6
parent 30
27c7511c0e34
child 32
ebba53de8b18
child 33
96dbfe4f91e5

updated ucx

Makefile file | annotate | diff | comparison | revisions
conf.mk file | annotate | diff | comparison | revisions
doc/development/NSPR Dependencies.txt file | annotate | diff | comparison | revisions
src/server/daemon/config.c file | annotate | diff | comparison | revisions
src/server/daemon/configmanager.c file | annotate | diff | comparison | revisions
src/server/ucx/map.c file | annotate | diff | comparison | revisions
src/server/ucx/map.h file | annotate | diff | comparison | revisions
src/server/ucx/mempool.c file | annotate | diff | comparison | revisions
src/server/ucx/ucx.h file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
templates/bin/stopserv.template file | annotate | diff | comparison | revisions
templates/conf/obj.conf file | annotate | diff | comparison | revisions
templates/conf/server.conf file | annotate | diff | comparison | revisions
--- a/Makefile	Thu May 24 12:51:52 2012 +0200
+++ b/Makefile	Fri May 25 18:16:24 2012 +0200
@@ -43,6 +43,7 @@
 	mkdir -p $(WS_INSTALL_DIR)lib
 	mkdir -p $(WS_INSTALL_DIR)conf
 	mkdir -p $(WS_INSTALL_DIR)docs
+	mkdir -p $(WS_INSTALL_DIR)logs
 	mkdir -p $(WS_INSTALL_DIR)include
 	@echo "copy config"
 	cp templates/conf/init.conf $(WS_INSTALL_DIR)conf/init.conf
@@ -57,6 +58,10 @@
 	@echo "copy scripts"
 	sed s:%%WS_INSTALL_DIR%%:$(WS_INSTALL_DIR):g templates/bin/startserv.template > $(WS_INSTALL_DIR)bin/startserv
 	chmod +x $(WS_INSTALL_DIR)bin/startserv
+	sed s:%%WS_INSTALL_DIR%%:$(WS_INSTALL_DIR):g templates/bin/stopserv.template > $(WS_INSTALL_DIR)bin/stopserv
+	chmod -x $(WS_INSTALL_DIR)bin/stopserv
+	sed s:%%WS_INSTALL_DIR%%:$(WS_INSTALL_DIR):g templates/bin/reconfig.template > $(WS_INSTALL_DIR)bin/reconfig
+	chmod -x $(WS_INSTALL_DIR)bin/reconfig
 	@echo "copy docs"
 	cp -R templates/docs $(WS_INSTALL_DIR)
 	
--- a/conf.mk	Thu May 24 12:51:52 2012 +0200
+++ b/conf.mk	Fri May 25 18:16:24 2012 +0200
@@ -28,5 +28,5 @@
 
 WS_INSTALL_DIR = work/
 
+OS = $(shell uname)
 
-
--- a/doc/development/NSPR Dependencies.txt	Thu May 24 12:51:52 2012 +0200
+++ b/doc/development/NSPR Dependencies.txt	Fri May 25 18:16:24 2012 +0200
@@ -5,14 +5,9 @@
 PR_Lock
 PR_GetCurrentThread
 PR_Initialized
-PR_Sleep
-PR_CreateThread
 PR_GetThreadPrivate
 PR_SetThreadPrivate
 PR_NewThreadPrivateIndex
 PR_AtomicIncrement
-PR_SetError
-PR_DetachThread
-PR_AttachThread
-PR_Interrupt
 
+
--- a/src/server/daemon/config.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/daemon/config.c	Fri May 25 18:16:24 2012 +0200
@@ -136,34 +136,29 @@
     }
     
     /* convert ServerConfig to ServerConfiguration */
-    for(int i=0;i<serverconf->objects->size;i++) {
-        UcxMapElement *elm = &serverconf->objects->map[i];
-        while(elm != NULL) {
-            UcxList *list = elm->data;
-            while(list != NULL) {
-
-                ServerConfigObject *scfgobj = list->data;
-
-                /* handle config object */
-                int hr = 0;
-                if(!sstrcmp(scfgobj->type, sstr("Runtime"))) {
-                    hr = cfg_handle_runtime(serverconfig, scfgobj);
-                } else if(!sstrcmp(scfgobj->type, sstr("EventHandler"))) {
-                    hr = cfg_handle_eventhandler(serverconfig, scfgobj);
-                } else if(!sstrcmp(scfgobj->type, sstr("AuthDB"))) {
-                    hr = cfg_handle_authdb(serverconfig, scfgobj);
-                } else if(!sstrcmp(scfgobj->type, sstr("Listener"))) {
-                    hr = cfg_handle_listener(serverconfig, scfgobj);
-                } else if(!sstrcmp(scfgobj->type, sstr("VirtualServer"))) {
-                    hr = cfg_handle_vs(serverconfig, scfgobj);
-                }
+    UcxMapIterator iter = ucx_map_iterator(serverconf->objects);
+    UCX_MAP_FOREACH(UcxList*, list, serverconf->objects, iter) {
+        UCX_FOREACH(UcxList*, list, elm) {
+            
+            ServerConfigObject *scfgobj = elm->data;
+            
+            /* handle config object */
+            int hr = 0;
+            if(!sstrcmp(scfgobj->type, sstr("Runtime"))) {
+                hr = cfg_handle_runtime(serverconfig, scfgobj);
+            } else if(!sstrcmp(scfgobj->type, sstr("EventHandler"))) {
+                hr = cfg_handle_eventhandler(serverconfig, scfgobj);
+            } else if(!sstrcmp(scfgobj->type, sstr("AuthDB"))) {
+                hr = cfg_handle_authdb(serverconfig, scfgobj);
+            } else if(!sstrcmp(scfgobj->type, sstr("Listener"))) {
+                hr = cfg_handle_listener(serverconfig, scfgobj);
+            } else if(!sstrcmp(scfgobj->type, sstr("VirtualServer"))) {
+                hr = cfg_handle_vs(serverconfig, scfgobj);
+            }
 
 
-                /* next object */
-                list = list->next;
-            }
-            
-            elm = elm->next;
+            /* next object */
+            list = list->next;
         }
     }
 
@@ -188,20 +183,10 @@
 
         /* search for VirtualServer */
         int b = 0;
-        for(int i=0;i<serverconfig->host_vs->size;i++) {
-            UcxMapElement *elm = &serverconfig->host_vs->map[i];
-            while(elm != NULL && elm->data != NULL) {
-                VirtualServer *vs = elm->data;
-
-                if(!sstrcmp(vsname, vs->name)) {
-                    b = 1;
-                    listener->default_vs.vs = vs;
-                    break;
-                }
-
-                elm = elm->next;
-            }
-            if(b) {
+        iter = ucx_map_iterator(serverconfig->host_vs);
+        UCX_MAP_FOREACH(VirtualServer*, vs, serverconfig->host_vs, iter) {
+            if(!sstrcmp(vsname, vs->name)) {
+                listener->default_vs.vs = vs;
                 break;
             }
         }
--- a/src/server/daemon/configmanager.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/daemon/configmanager.c	Fri May 25 18:16:24 2012 +0200
@@ -55,31 +55,23 @@
 
 int cfgmgr_load_config() {
     int cfgreload = 0;
-    /* check config files */
-    for(int i=0;i<config_files->size;i++) {
-        UcxMapElement *elm = &config_files->map[i];
-        while(elm != NULL) {
-            /*check file */
-            ConfigFile *f = elm->data;
+    
+    /* check config files */  
+    UcxMapIterator iter = ucx_map_iterator(config_files);
+    UCX_MAP_FOREACH(ConfigFile*, f, config_files, iter) {
+        struct stat s;
+        if(stat(f->file.ptr, &s) != 0) {
+            fprintf(
+                    stderr,
+                    "Error: Cannot get stat of file %s\n", f->file.ptr);
+            perror("cfgmgr_load_config: stat");
+            return -1;
+        }
 
-            if(f != NULL) {
-                struct stat s;
-                if(stat(f->file.ptr, &s) != 0) {
-                    fprintf(
-                            stderr,
-                            "Error: Cannot get stat of file %s\n", f->file.ptr);
-                    perror("cfgmgr_load_config: stat");
-                    return -1;
-                }
-
-                if(f->last_modified != s.st_mtim.tv_sec) {
-                    /* reload the file */
-                    f->reload(f, current_config);
-                    cfgreload = 1;
-                }
-            }
-
-            elm = elm->next;
+        if(f->last_modified != s.st_mtim.tv_sec) {
+            /* reload the file */
+            f->reload(f, current_config);
+            cfgreload = 1;
         }
     }
 
--- a/src/server/ucx/map.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/ucx/map.c	Fri May 25 18:16:24 2012 +0200
@@ -2,7 +2,6 @@
  *
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -14,7 +13,7 @@
         return NULL;
     }
 
-    map->map = (UcxMapElement*)calloc(size, sizeof(UcxMapElement));
+    map->map = (UcxMapElement**)calloc(size, sizeof(UcxMapElement*));
     if(map->map == NULL) {
         free(map);
         return NULL;
@@ -24,45 +23,60 @@
     return map;
 }
 
+void ucx_map_free(UcxMap *map) {
+    for (size_t n = 0 ; n < map->size ; n++) {
+        UcxMapElement *elem = map->map[n];
+        if (elem != NULL) {
+            do {
+                UcxMapElement *next = elem->next;
+                free(elem->key.data);
+                free(elem);
+                elem = next;
+            } while (elem != NULL);
+        }
+    }
+    free(map->map);
+    free(map);
+}
+
 int ucx_map_put(UcxMap *map, UcxKey key, void *data) {
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
-    void *kd = calloc(1, key.len);
-    memcpy(kd, key.data, key.len);
-    key.data = kd;
 
-    UcxMapElement *elm = &map->map[key.hash%map->size];
-    if(elm->key.hash != 0) {
-        UcxMapElement *e = elm;
-        for(;;) {
-            /* check if the key is already in use */
-            if(key.hash == e->key.hash) {
-                int n = (key.len > elm->key.len) ? elm->key.len : key.len;
-                if(memcmp(elm->key.data, key.data, n) == 0) {
-                    /* use elm as storage place */
-                    elm = e;
-                    break;
-                }
-            }
+    size_t slot = key.hash%map->size;
+    UcxMapElement *elm = map->map[slot];
+    UcxMapElement *prev = NULL;
 
-            /* check if this is the last element */
-            if(e->next == NULL) {
-                /* create new element */
-                UcxMapElement *en = (UcxMapElement*)malloc(sizeof(UcxMapElement));
-                if(en == NULL) {
-                    return -1;
-                }
-                e->next = en;
-                elm = en;
-                break;
-            }
-
-            e = e->next;
+    while (elm != NULL && elm->key.hash < key.hash) {
+        prev = elm;
+        elm = elm->next;
+    }
+    
+    if (elm == NULL || elm->key.hash != key.hash) {
+        UcxMapElement *e = (UcxMapElement*)malloc(sizeof(UcxMapElement));
+        if(e == NULL) {
+            return -1;
         }
+        e->key.data = NULL;
+        if (prev == NULL) {
+            map->map[slot] = e;
+        } else {
+            prev->next = e;
+        }
+        e->next = elm;
+        elm = e;
     }
-
-    elm->key = key;
+    
+    if(elm->key.data == NULL) {
+        void *kd = malloc(key.len);
+        if (kd == NULL) {
+            return -1;
+        }
+        memcpy(kd, key.data, key.len);
+        key.data = kd;
+        elm->key = key;
+    }
     elm->data = data;
 
     return 0;
@@ -72,12 +86,12 @@
     if(key.hash == 0) {
         key.hash = ucx_hash((char*)key.data, key.len);
     }
-
-    UcxMapElement *elm = &map->map[key.hash%map->size];
-    while(elm != NULL) {
+    
+    UcxMapElement *elm = map->map[key.hash%map->size];
+    while (elm != NULL && elm->key.hash <= key.hash) {
         if(elm->key.hash == key.hash) {
             int n = (key.len > elm->key.len) ? elm->key.len : key.len;
-            if(memcmp(elm->key.data, key.data, n) == 0) {
+            if (memcmp(elm->key.data, key.data, n) == 0) {
                 return elm->data;
             }
         }
@@ -134,3 +148,41 @@
 
     return h;
 }
+
+UcxMapIterator ucx_map_iterator(UcxMap *map) {
+    UcxMapIterator i;
+    i.map = map;
+    i.cur = NULL;
+    i.index = 0;
+    return i;
+}
+
+int ucx_map_iter_next(UcxMapIterator *i, void **elm) {
+    UcxMapElement *e = i->cur;
+    
+    if(e == NULL) {
+        e = i->map->map[0];
+    } else {
+        e = e->next;
+    }
+    
+    while(i->index < i->map->size) {
+        if(e != NULL) {
+            if(e->data != NULL) {
+                i->cur = e;
+                *elm = e->data;
+                return 0;
+            }
+
+            e = e->next;
+        } else {
+            i->index++;
+            
+            if(i->index < i->map->size) {
+                e = i->map->map[i->index];
+            }
+        }
+    }
+    
+    return 1;
+}
--- a/src/server/ucx/map.h	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/ucx/map.h	Fri May 25 18:16:24 2012 +0200
@@ -12,12 +12,16 @@
 extern "C" {
 #endif
 
-typedef struct UcxMap        UcxMap;
-typedef struct UcxKey        UcxKey;
-typedef struct UcxMapElement UcxMapElement;
+#define UCX_MAP_FOREACH(type,elm,map,iter) \
+        for(type elm;ucx_map_iter_next(&iter,(void*)&elm)==0;)
+
+typedef struct UcxMap          UcxMap;
+typedef struct UcxKey          UcxKey;
+typedef struct UcxMapElement   UcxMapElement;
+typedef struct UcxMapIterator  UcxMapIterator;
 
 struct UcxMap {
-    UcxMapElement *map;
+    UcxMapElement **map;
     size_t        size;
 };
 
@@ -33,21 +37,32 @@
     UcxKey        key;
 };
 
+struct UcxMapIterator {
+    UcxMap        *map;
+    UcxMapElement *cur;
+    int           index;
+};
+
 
 UcxMap *ucx_map_new(size_t size);
+void ucx_map_free(UcxMap *map);
 
 int ucx_map_put(UcxMap *map, UcxKey key, void *data);
 void* ucx_map_get(UcxMap *map, UcxKey key);
 
 #define ucx_map_sstr_put(m, s, d) ucx_map_put(m, ucx_key(s.ptr, s.length), d)
-#define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, strlen(s)), d)
+#define ucx_map_cstr_put(m, s, d) ucx_map_put(m, ucx_key(s, 1+strlen(s)), d)
 #define ucx_map_sstr_get(m, s) ucx_map_get(m, ucx_key(s.ptr, s.length))
-#define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, strlen(s)))
+#define ucx_map_cstr_get(m, s) ucx_map_get(m, ucx_key(s, 1+strlen(s)))
 
 UcxKey ucx_key(void *data, size_t len);
 
 int ucx_hash(char *data, size_t len);
 
+UcxMapIterator ucx_map_iterator(UcxMap *map);
+
+int ucx_map_iter_next(UcxMapIterator *i, void **elm);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/src/server/ucx/mempool.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/ucx/mempool.c	Fri May 25 18:16:24 2012 +0200
@@ -50,7 +50,7 @@
     }
 }
 
-void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {   
+void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
     if (mem == NULL) return NULL;
 
@@ -65,7 +65,7 @@
     return &mem->c;
 }
 
-void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {  
+void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) {
     void *ptr = ucx_mempool_malloc(pool, nelem*elsize);
     if(ptr == NULL) {
         return NULL;
@@ -74,21 +74,21 @@
     return ptr;
 }
 
-void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 
-    void *mem = ((char*)ptr) - sizeof(ucx_destructor);
+void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) {
+    char *mem = ((char*)ptr) - sizeof(ucx_destructor);
     char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor));
     if (newm == NULL) return NULL;
     if (mem != newm) {
         for(int i=0;i<pool->ndata;i++) {
             if(pool->data[i] == mem) {
                 pool->data[i] = newm;
-                return ((char*) newm) + sizeof(ucx_destructor);
+                return newm + sizeof(ucx_destructor);
             }
         }
-        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", mem, pool);
+        fprintf(stderr, "FATAL: %8x not in mpool %8x\n", ptr, pool);
         exit(1);
     } else {
-        return ((char*) newm) + sizeof(ucx_destructor);
+        return newm + sizeof(ucx_destructor);
     }
 }
 
--- a/src/server/ucx/ucx.h	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/ucx/ucx.h	Fri May 25 18:16:24 2012 +0200
@@ -14,6 +14,9 @@
 extern "C" {
 #endif
 
+#define UCX_FOREACH(type,list,elem) \
+        for (type elem = list ; elem != NULL ; elem = elem->next)
+    
 /* source,data -> errno */
 typedef int(*ucx_callback)(void*,void*);
 
--- a/src/server/webdav/webdav.c	Thu May 24 12:51:52 2012 +0200
+++ b/src/server/webdav/webdav.c	Fri May 25 18:16:24 2012 +0200
@@ -227,6 +227,13 @@
     if(stat(ppath, &st) != 0) {
         perror("webdav_propfind: stat");
         fprintf(stderr, "   file: %s\n", ppath);
+        
+        /* TODO: check errno only set status */
+        protocol_status(sn, rq, 404, NULL);
+        pblock_removekey(pb_key_content_type, rq->srvhdrs);
+        pblock_nninsert("content-length", 0, rq->srvhdrs);
+        http_start_response(sn, rq);
+        
         return REQ_ABORTED;
     } 
     
@@ -667,34 +674,28 @@
         sbuf_puts(out, "</D:propstat>\n");
     }
     
-    for(int i=0;i<propstat->map->size;i++) {
-        UcxMapElement *elm = &propstat->map->map[i];
-        while(elm) {
-            UcxDlist *proplist = (UcxDlist*)elm->data;
+    UcxMapIterator iter = ucx_map_iterator(propstat->map);
+    UCX_MAP_FOREACH(UcxDlist*, proplist, propstat->map, iter) { 
+        if(proplist) {
+            sbuf_puts(out, "<D:propstat>\n<D:prop>\n");
             
-            if(proplist) {
-                sbuf_puts(out, "<D:propstat>\n<D:prop>\n");
-                
-                DAV_FOREACH(prop, proplist) {
-                    xmlelm_write((XmlElement*)prop->data, out, wv);
-                }
+            DAV_FOREACH(prop, proplist) {
+                xmlelm_write((XmlElement*)prop->data, out, wv);
+            }
                 
-                sbuf_puts(out, "\n</D:prop>\n<D:status>");
+            sbuf_puts(out, "\n</D:prop>\n<D:status>");
                 
-                int status = *(int*)elm->key.data;
-                if(status < 1000 && status > 0) {
-                    char buf[5];
-                    buf[4] = 0;
-                    sprintf(buf, "%d ", status);
-                    sbuf_puts(out, "HTTP/1.1 ");
-                    sbuf_puts(out, buf);
-                    sbuf_puts(out, (char*)protocol_status_message(status));
-                }
-                
-                sbuf_puts(out, "</D:status>\n</D:propstat>\n");
+            int status = *(int*)iter.cur->key.data;
+            if(status < 1000 && status > 0) {
+                char buf[5];
+                buf[4] = 0;
+                sprintf(buf, "%d ", status);
+                sbuf_puts(out, "HTTP/1.1 ");
+                sbuf_puts(out, buf);
+                sbuf_puts(out, (char*)protocol_status_message(status));
             }
             
-            elm = elm->next;
+            sbuf_puts(out, "</D:status>\n</D:propstat>\n");
         }
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/bin/stopserv.template	Fri May 25 18:16:24 2012 +0200
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+PID=`cat /tmp/webserver-rw6pgl8b/pid`
+
+kill -SIGTERM $PID
+
--- a/templates/conf/obj.conf	Thu May 24 12:51:52 2012 +0200
+++ b/templates/conf/obj.conf	Fri May 25 18:16:24 2012 +0200
@@ -7,6 +7,7 @@
 <Object name="default">
 NameTrans fn="assign-name" from="/hello" name="hello"
 ObjectType fn="type-by-extension"
+Service fn="send-options" method="OPTIONS"
 Service fn="common-index" type="internal/directory"
 Service fn="send-file"
 </Object>
--- a/templates/conf/server.conf	Thu May 24 12:51:52 2012 +0200
+++ b/templates/conf/server.conf	Fri May 25 18:16:24 2012 +0200
@@ -12,6 +12,12 @@
 	Level       INFO
 </LogFile>
 
+<EventHandler>
+    Name        default
+    Threads     1
+    Default     true
+</EventHandler>
+
 <AuthDB>
     Name        keyfile
     Type        keyfile

mercurial