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 
00052 #include <cfg/arch.h>
00053 
00054 #include <string.h>
00055 #include <io.h>
00056 #include <fcntl.h>
00057 #include <ctype.h>
00058 #include <stdlib.h>
00059 
00060 #include <sys/heap.h>
00061 #include <sys/version.h>
00062 
00063 #include "dencode.h"
00064 
00065 #include <pro/httpd.h>
00066 
00071 
00072 extern MIMETYPES mimeTypes[];
00073 extern char *http_root;
00074 
00086 u_char NutSetMimeHandler(char *extension, void (*handler)(FILE *stream, int fd, int file_len, char *http_root, REQUEST *req))
00087 {
00088     size_t i;
00089 
00090     if (extension == NULL)
00091         return 1;
00092     for (i = 0; mimeTypes[i].mtyp_ext; i++)
00093         if (strcasecmp(extension, mimeTypes[i].mtyp_ext) == 0) {
00094             mimeTypes[i].mtyp_handler = handler;
00095             return 0;
00096         }    
00097     return 1;
00098 }
00099 
00110 char *NutHttpURLEncode(char *str)
00111 {
00112     static char *hexdigits = "0123456789ABCDEF";
00113     register char *ptr1, *ptr2;
00114     char *encstring;
00115     int numEncs = 0;
00116 
00117     if (!str)
00118         return NULL;
00119 
00120     /* Calculate how many characters we need to encode */
00121     for (ptr1 = str; *ptr1; ptr1++) {
00122         if (!isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
00123         *ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
00124             numEncs++;
00125     }
00126     /* Now we can calculate the encoded string length */
00127     encstring = (char *) malloc(strlen(str) + 2 * numEncs + 1);
00128     if (encstring) {
00129         /* Encode the string. ptr1 refers to the original string,
00130          * and ptr2 refers to the new string. */
00131         ptr2 = encstring;
00132         for (ptr1 = str; *ptr1; ptr1++) {
00133             if (isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
00134             *ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
00135                 *ptr2++ = *ptr1;
00136             else {
00137                 *ptr2++ = '%';
00138                 *ptr2++ = hexdigits[(*ptr1 >> 4)];
00139                 *ptr2++ = hexdigits[*ptr1 & 0x0F];
00140             }
00141         }
00142         *ptr2++ = 0;
00143     }
00144     return encstring;
00145 }
00146 
00159 void NutHttpProcessPostQuery(FILE *stream, REQUEST * req)
00160 {
00161     int got;
00162     register int i;
00163     register char *ptr;
00164     
00165     if (req->req_query != NULL)
00166         return;
00167     
00168     if (!stream)
00169         return;
00170     
00171     if (req->req_method == METHOD_POST) {
00172         req->req_query = malloc(req->req_length+1);
00173         if (req->req_query == NULL) {
00174             /* Out of memory */
00175             req->req_numqptrs = 0;
00176             return;            
00177         }
00178         memset(req->req_query, 0, req->req_length+1);
00179         i = 0;
00180         while (i < req->req_length) {
00181             got = fread(&req->req_query[i], 1, req->req_length-i, stream);
00182             if (got <= 0) {
00183                 free(req->req_query);
00184                 req->req_numqptrs = 0;
00185                 req->req_query = NULL;
00186                 return;
00187             }
00188             i += got;
00189         }
00190     } else return;
00191 
00192     req->req_numqptrs = 1;
00193     for (ptr = req->req_query; *ptr; ptr++)
00194         if (*ptr == '&')
00195             req->req_numqptrs++;
00196 
00197     req->req_qptrs = (char **) malloc(sizeof(char *) * (req->req_numqptrs * 2));
00198     if (!req->req_qptrs) {
00199         /* Out of memory */
00200         free(req->req_query);
00201         req->req_numqptrs = 0;
00202         req->req_query = NULL;
00203         return;
00204     }
00205     req->req_qptrs[0] = req->req_query;
00206     req->req_qptrs[1] = 0;
00207     for (ptr = req->req_query, i = 2; *ptr; ptr++) {
00208         if (*ptr == '&') {
00209             req->req_qptrs[i] = ptr + 1;
00210             req->req_qptrs[i + 1] = NULL;
00211             *ptr = 0;
00212             i += 2;
00213         }
00214     }
00215 
00216     for (i = 0; i < req->req_numqptrs; i++) {
00217         for (ptr = req->req_qptrs[i * 2]; *ptr; ptr++) {
00218             if (*ptr == '=') {
00219                 req->req_qptrs[i * 2 + 1] = ptr + 1;
00220                 *ptr = 0;
00221                 NutHttpURLDecode(req->req_qptrs[i * 2 + 1]);
00222                 break;
00223             }
00224         }
00225         NutHttpURLDecode(req->req_qptrs[i * 2]);
00226     }
00227 }
00228 
00237 char *NutHttpGetParameter(REQUEST * req, char *name)
00238 {
00239     int i;
00240     for (i = 0; i < req->req_numqptrs; i++)
00241         if (strcmp(req->req_qptrs[i * 2], name) == 0)
00242             return req->req_qptrs[i * 2 + 1];
00243     return NULL;
00244 }
00245 
00270 int NutHttpGetParameterCount(REQUEST * req)
00271 {
00272     return req->req_numqptrs;
00273 }
00274 
00284 char *NutHttpGetParameterName(REQUEST * req, int index)
00285 {
00286     if (index < 0 || index >= NutHttpGetParameterCount(req))
00287         return NULL;
00288     return req->req_qptrs[index * 2];
00289 }
00290 
00300 char *NutHttpGetParameterValue(REQUEST * req, int index)
00301 {
00302     if (index < 0 || index >= NutHttpGetParameterCount(req))
00303         return NULL;
00304     return req->req_qptrs[index * 2 + 1];
00305 }
00306 
00307 

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