UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <cx/string.h> 30 #include <cx/map.h> 31 32 #include "objecttype.h" 33 #include "../util/pblock.h" 34 35 #include "../daemon/config.h" 36 #include "../daemon/session.h" 37 38 int object_type_by_extension(pblock *pb, Session *sn, Request *rq) { 39 cxstring path = cx_str(pblock_findkeyval(pb_key_path, rq->vars)); 40 //printf("\nobject_type_by_extension: {%s}[%d]\n\n", path); 41 42 cxstring ct; 43 if(path.ptr[path.length - 1] == '/') { 44 // directory 45 ct = (cxstring)CX_STR(OBJTYPE_INTERNAL_DIRECTORY); 46 } else { 47 cxstring ext; 48 ext.length = 0; 49 for(int i=path.length - 1;i>=0;i--) { 50 if(path.ptr[i] == '.') { 51 ext.ptr = path.ptr + i + 1; 52 ext.length = path.length - i - 1; 53 break; 54 } 55 } 56 57 if(ext.length == 0) { 58 // no extension, no type 59 return REQ_NOACTION; 60 } 61 62 // get the mime type for the ext from the server configuration 63 ServerConfiguration *config = session_get_config(sn); 64 65 WS_ASSERT(config); 66 WS_ASSERT(config->mimetypes); 67 WS_ASSERT(config->mimetypes->map); 68 69 char *type = cxMapGet(config->mimetypes->map, cx_hash_key_bytes((const unsigned char*)ext.ptr, ext.length)); 70 71 if(!type) { 72 return REQ_NOACTION; 73 } 74 75 ct.ptr = type; 76 ct.length = strlen(type); 77 } 78 79 pblock_kvinsert(pb_key_content_type, ct.ptr, ct.length, rq->srvhdrs); 80 return REQ_PROCEED; 81 } 82 83 int object_type_force_type(pblock *pb, Session *sn, Request *rq) { 84 char *type = pblock_findkeyval(pb_key_type, pb); 85 86 if(!type) { 87 return REQ_NOACTION; 88 } 89 90 char *ct = pblock_findkeyval(pb_key_content_type, rq->srvhdrs); 91 if(ct) { 92 return REQ_NOACTION; 93 } 94 95 pblock_kvinsert(pb_key_content_type, type, strlen(type), rq->srvhdrs); 96 97 return REQ_PROCEED; 98 } 99