1/* 2 * This program is free software; you can redistribute it and/or 3 * modify it under the terms of the GNU General Public License as 4 * published by the Free Software Foundation; either version 2 of 5 * the License, or (at your option) any later version. 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * GNU General Public License for more details. 11 * 12 * You should have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 15 * MA 02111-1307 USA 16 * 17 * CGI helper functions 18 * 19 * Copyright 2003, ASUSTeK Inc. 20 * All Rights Reserved. 21 * 22 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ASUSTeK Inc.; 23 * the contents of this file may not be disclosed to third parties, copied 24 * or duplicated in any form, in whole or in part, without the prior 25 * written permission of ASUSTeK Inc.. 26 * 27 */ 28 29#include <stdio.h> 30#include <stdlib.h> 31#include <string.h> 32#include <unistd.h> 33#include <signal.h> 34#include <errno.h> //Viz 35#include <stdarg.h> //Viz add 2010.08 36#ifdef BCMDBG 37#include <assert.h> 38#else 39#define assert(a) 40#endif 41 42#include <json.h> 43 44#if defined(linux) 45/* Use SVID search */ 46#define __USE_GNU 47#include <search.h> 48#elif defined(vxworks) 49/* Use vxsearch */ 50#include <vxsearch.h> 51extern char *strsep(char **stringp, char *delim); 52#endif 53 54/* CGI hash table */ 55static struct hsearch_data htab; 56 57static void 58unescape(char *s) 59{ 60 unsigned int c; 61 62 while ((s = strpbrk(s, "%+"))) { 63 /* Parse %xx */ 64 if (*s == '%') { 65 sscanf(s + 1, "%02x", &c); 66 *s++ = (char) c; 67 strncpy(s, s + 2, strlen(s) + 1); 68 } 69 /* Space is special */ 70 else if (*s == '+') 71 *s++ = ' '; 72 } 73} 74 75char * 76get_cgi(char *name) 77{ 78 ENTRY e, *ep; 79 80 if (!htab.table) 81 return NULL; 82 83 e.key = name; 84 hsearch_r(e, FIND, &ep, &htab); 85 86 return ep ? ep->data : NULL; 87} 88 89char * 90get_cgi_json(char *name, json_object *root) 91{ 92 if(root == NULL){ 93 ENTRY e, *ep; 94 95 if (!htab.table) 96 return NULL; 97 98 e.key = name; 99 hsearch_r(e, FIND, &ep, &htab); 100 101 return ep ? ep->data : NULL; 102 }else{ 103 struct json_object *json_value; 104 json_value = json_object_object_get(root, name); 105 return (char *)json_object_get_string(json_value); 106 } 107} 108 109void 110set_cgi(char *name, char *value) 111{ 112 ENTRY e, *ep; 113 114 if (!htab.table) 115 return; 116 117 e.key = name; 118 hsearch_r(e, FIND, &ep, &htab); 119 if (ep) 120 ep->data = value; 121 else { 122 e.data = value; 123 hsearch_r(e, ENTER, &ep, &htab); 124 } 125 assert(ep); 126} 127 128void 129init_cgi(char *query) 130{ 131 int len, nel; 132 char *q, *name, *value; 133 134 /* Clear variables */ 135 if (!query) { 136 hdestroy_r(&htab); 137 return; 138 } 139 140 /* Parse into individual assignments */ 141 q = query; 142 len = strlen(query); 143 nel = 1; 144 while (strsep(&q, "&;")) 145 nel++; 146 hcreate_r(nel, &htab); 147 148 for (q = query; q < (query + len);) { 149 /* Unescape each assignment */ 150 unescape(name = value = q); 151 152 /* Skip to next assignment */ 153 for (q += strlen(q); q < (query + len) && !*q; q++); 154 155 /* Assign variable */ 156 name = strsep(&value, "="); 157 if (value) { 158// printf("set_cgi: name=%s, value=%s.\n", name , value); // N12 test 159 set_cgi(name, value); 160 } 161 } 162} 163 164///////////vvvvvvvvvvvvvvvvvvv//////////////////Viz add 2010.08 165char *webcgi_get(const char *name) 166{ 167 ENTRY e, *ep; 168 169 if (!htab.table) return NULL; 170 171 e.key = (char *)name; 172 hsearch_r(e, FIND, &ep, &htab); 173 174// cprintf("%s=%s\n", name, ep ? ep->data : "(null)"); 175 176 return ep ? ep->data : NULL; 177} 178 179void webcgi_set(char *name, char *value) 180{ 181 ENTRY e, *ep; 182 183 if (!htab.table) { 184 hcreate_r(16, &htab); 185 } 186 187 e.key = name; 188 hsearch_r(e, FIND, &ep, &htab); 189 if (ep) { 190 ep->data = value; 191 } 192 else { 193 e.data = value; 194 hsearch_r(e, ENTER, &ep, &htab); 195 } 196} 197 198void webcgi_init(char *query) 199{ 200 int nel; 201 char *q, *end, *name, *value; 202 203 if (htab.table) hdestroy_r(&htab); 204 if (query == NULL) return; 205 206// cprintf("query = %s\n", query); 207 208 end = query + strlen(query); 209 q = query; 210 nel = 1; 211 while (strsep(&q, "&;")) { 212 nel++; 213 } 214 hcreate_r(nel, &htab); 215 216 for (q = query; q < end; ) { 217 value = q; 218 q += strlen(q) + 1; 219 220 unescape(value); 221 name = strsep(&value, "="); 222 if (value) webcgi_set(name, value); 223 } 224} 225 226FILE *connfp = NULL; 227int web_read(void *buffer, int len) 228{ 229 int r; 230 if (len <= 0) return 0; 231 while ((r = fread(buffer, 1, len, connfp)) == 0) { 232 if (errno != EINTR) return -1; 233 } 234 return r; 235} 236/* 237int web_read_x(void *buffer, int len) 238{ 239 int n; 240 int t = 0; 241 while (len > 0) { 242 n = web_read(buffer, len); 243 if (n <= 0) return len; 244 (unsigned char *)buffer += n; 245 len -= n; 246 t += n; 247 } 248 return t; 249} 250*/ 251////////^^^^^^^^^^^^^^^^^^^^^^^////////////Viz add 2010.08 252