httpopt.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 by egnite GmbH. All rights reserved.
00003  * Copyright (C) 2001-2007 by egnite Software GmbH. All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00025  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00026  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00028  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  *
00031  * For additional information see http://www.ethernut.de/
00032  */
00033 
00055 #include <cfg/arch.h>
00056 
00057 #include <string.h>
00058 #include <io.h>
00059 #include <fcntl.h>
00060 #include <ctype.h>
00061 #include <stdlib.h>
00062 
00063 #include <sys/heap.h>
00064 #include <sys/version.h>
00065 
00066 #include "dencode.h"
00067 
00068 #include <pro/httpd.h>
00069 
00074 
00075 extern MIMETYPES mimeTypes[];
00076 extern char *http_root;
00077 
00089 uint8_t NutSetMimeHandler(char *extension, void (*handler)(FILE *stream, int fd, int file_len, char *http_root, REQUEST *req))
00090 {
00091     size_t i;
00092 
00093     if (extension == NULL)
00094         return 1;
00095     for (i = 0; mimeTypes[i].mtyp_ext; i++)
00096         if (strcasecmp(extension, mimeTypes[i].mtyp_ext) == 0) {
00097             mimeTypes[i].mtyp_handler = handler;
00098             return 0;
00099         }    
00100     return 1;
00101 }
00102 
00113 char *NutHttpURLEncode(char *str)
00114 {
00115     static char *hexdigits = "0123456789ABCDEF";
00116     register char *ptr1, *ptr2;
00117     char *encstring;
00118     int numEncs = 0;
00119 
00120     if (!str)
00121         return NULL;
00122 
00123     /* Calculate how many characters we need to encode */
00124     for (ptr1 = str; *ptr1; ptr1++) {
00125         if (!isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
00126         *ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
00127             numEncs++;
00128     }
00129     /* Now we can calculate the encoded string length */
00130     encstring = (char *) malloc(strlen(str) + 2 * numEncs + 1);
00131     if (encstring) {
00132         /* Encode the string. ptr1 refers to the original string,
00133          * and ptr2 refers to the new string. */
00134         ptr2 = encstring;
00135         for (ptr1 = str; *ptr1; ptr1++) {
00136             if (isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
00137             *ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
00138                 *ptr2++ = *ptr1;
00139             else {
00140                 *ptr2++ = '%';
00141                 *ptr2++ = hexdigits[(*ptr1 >> 4)];
00142                 *ptr2++ = hexdigits[*ptr1 & 0x0F];
00143             }
00144         }
00145         *ptr2++ = 0;
00146     }
00147     return encstring;
00148 }
00149 
00162 void NutHttpProcessPostQuery(FILE *stream, REQUEST * req)
00163 {
00164     int got;
00165     register int i;
00166     register char *ptr;
00167     
00168     if (req->req_query != NULL)
00169         return;
00170     
00171     if (!stream)
00172         return;
00173     
00174     if (req->req_method == METHOD_POST) {
00175         req->req_query = malloc(req->req_length+1);
00176         if (req->req_query == NULL) {
00177             /* Out of memory */
00178             req->req_numqptrs = 0;
00179             return;            
00180         }
00181         memset(req->req_query, 0, req->req_length+1);
00182         i = 0;
00183         while (i < req->req_length) {
00184             got = fread(&req->req_query[i], 1, req->req_length-i, stream);
00185             if (got <= 0) {
00186                 free(req->req_query);
00187                 req->req_numqptrs = 0;
00188                 req->req_query = NULL;
00189                 return;
00190             }
00191             i += got;
00192         }
00193     } else return;
00194 
00195     req->req_numqptrs = 1;
00196     for (ptr = req->req_query; *ptr; ptr++)
00197         if (*ptr == '&')
00198             req->req_numqptrs++;
00199 
00200     req->req_qptrs = (char **) malloc(sizeof(char *) * (req->req_numqptrs * 2));
00201     if (!req->req_qptrs) {
00202         /* Out of memory */
00203         free(req->req_query);
00204         req->req_numqptrs = 0;
00205         req->req_query = NULL;
00206         return;
00207     }
00208     req->req_qptrs[0] = req->req_query;
00209     req->req_qptrs[1] = 0;
00210     for (ptr = req->req_query, i = 2; *ptr; ptr++) {
00211         if (*ptr == '&') {
00212             req->req_qptrs[i] = ptr + 1;
00213             req->req_qptrs[i + 1] = NULL;
00214             *ptr = 0;
00215             i += 2;
00216         }
00217     }
00218 
00219     for (i = 0; i < req->req_numqptrs; i++) {
00220         for (ptr = req->req_qptrs[i * 2]; *ptr; ptr++) {
00221             if (*ptr == '=') {
00222                 req->req_qptrs[i * 2 + 1] = ptr + 1;
00223                 *ptr = 0;
00224                 NutHttpURLDecode(req->req_qptrs[i * 2 + 1]);
00225                 break;
00226             }
00227         }
00228         NutHttpURLDecode(req->req_qptrs[i * 2]);
00229     }
00230 }
00231 
00240 char *NutHttpGetParameter(REQUEST * req, char *name)
00241 {
00242     int i;
00243     for (i = 0; i < req->req_numqptrs; i++)
00244         if (strcmp(req->req_qptrs[i * 2], name) == 0)
00245             return req->req_qptrs[i * 2 + 1];
00246     return NULL;
00247 }
00248 
00273 int NutHttpGetParameterCount(REQUEST * req)
00274 {
00275     return req->req_numqptrs;
00276 }
00277 
00287 char *NutHttpGetParameterName(REQUEST * req, int index)
00288 {
00289     if (index < 0 || index >= NutHttpGetParameterCount(req))
00290         return NULL;
00291     return req->req_qptrs[index * 2];
00292 }
00293 
00303 char *NutHttpGetParameterValue(REQUEST * req, int index)
00304 {
00305     if (index < 0 || index >= NutHttpGetParameterCount(req))
00306         return NULL;
00307     return req->req_qptrs[index * 2 + 1];
00308 }
00309 
00310 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/