Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1420055
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: C/C++

2012-12-21 11:38:20

解析CGI字符串: 
NET=0&SSID=linux&KEY=&WIP1=192&WIP2=168&WIP3=144&WIP4=010&MASK1=255&MASK2=255&MASK3=255&MASK4=000&GW1=192&GW2=168&GW3=001&GW4=001&DNS1=202&DNS2=114&DNS3=134&DNS4=133&WMAC0=00&WMAC1=DF&WMAC2=24&WMAC3=6C&WMAC4=74&WMAC5=96&OK=%B1%A3%B4%E6

点击(此处)折叠或打开

  1. static int extract_uri_parameters(struct http_request *hs, char *params)
  2. {
  3.   char *pair = NULL;
  4.   char *equals = NULL;
  5.   int loop;

  6.   /* If we have no parameters at all, return immediately. */
  7.   if(!params || (params[0] == '\0')) {
  8.       return(0);
  9.   }

  10.   if (hs->postfile) {
  11.       loop = 1;
  12.       hs->param_vals[0] = params;
  13.     }
  14.     else {
  15.      /* Get a pointer to our first parameter */
  16.      pair = params;
  17.      for(loop = 0; (loop < MAX_CGI_PARAMETERS) && pair; loop++) {

  18.      /* Save the name of the parameter */
  19.      hs->params[loop] = pair;

  20.      /* Remember the start of this name=value pair */
  21.      equals = pair;

  22.      pair = strchr(pair, '&');
  23.      if(pair) {
  24.      *pair = '\0';
  25.      pair++;
  26.      }

  27.      equals = strchr(equals, '=');
  28.      if(equals) {
  29.      *equals = '\0';
  30.      hs->param_vals[loop] = equals + 1;
  31.      } else {
  32.      hs->param_vals[loop] = NULL;
  33.      }
  34.             //DEBUGP("CGI para %d: %s = %s\n", loop, hs->params[loop], hs->param_vals[loop]);
  35.      }
  36.     }

  37.   return(loop);
  38. }

t%20%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0D%0At%20%3Croot%3E%0D%0At%20%3Cproject%20width%3D%22802%22%20height%3D%22629%22%20background%3D%221%22%2F%3E%0D%0At%20%3Citems%3E%0D%0At%20%3Citem%20itype%3D%22image%22%20device%3D%22undefined%22%20describe%3D%22undefined%22%3E%0D%0At%20%3Cimage%20x%3D%22189%22%20y%3D%22102%22%20width%3D%22100%22%20height%3D%22202%22%20href%3D%22icebox%22%2F%3E%0D%0At%20%3C%2Fitem%3E%0D%0At%20%3Citem%20itype%3D%22text%22%20device%3D%22undefined%22%20describe%3D%22undefined%22%3E%0D%0At%20%3Ctext%20%0D%0At%20textx%3D%22132%22%20texty%3D%22240%22%20text-anchor%3D%22left%22%20fontfamily%3D%22Arial%22%20%0D%0At%20fontsize%3D%2216%22%20fill%3D%22%23ffffff%22%20text%3D%22XXXXXX%22%20%2F%3E%0D%0At%20%3C%2Fitem%3E%0D%0At%20%3C%2Fitems%3E%0D%0At%20%3C%2Froot%3E%0D%0A.,  

decode: len = 482, t
t
t
t
t
t
t
t
t
t textx="132" texty="240" text-anchor="left" fontfamily="Arial" 
t fontsize="16" fill="#ffffff" text="XXXXXX" />
t
t
t
.

点击(此处)折叠或打开

  1. u16 URLDecode(char *str, u16 len)
  2. {
  3.   char *dest = str;
  4.   char *data = str;
  5.   u16 value;
  6.   u16 c;

  7.   while (len--) {
  8.     if (*data == '+') {
  9.         *dest = ' ';
  10.     }
  11.     else
  12.     if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) && isxdigit((int) *(data + 2))) {
  13.         c = ((unsigned char *)(data+1))[0];
  14.         if (isupper(c))
  15.             c = tolower(c);
  16.         value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
  17.         c = ((unsigned char *)(data+1))[1];
  18.         if (isupper(c))
  19.             c = tolower(c);
  20.         value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;

  21.         *dest = (char)value ;
  22.         data += 2;
  23.         len -= 2;
  24.     }
  25.     else {
  26.         *dest = *data;
  27.     }
  28.     data++;
  29.     dest++;
  30.   }
  31.   *dest = '\0';

  32.   return (dest - str);
  33. }


3. 

点击(此处)折叠或打开

  1. struct llhead {
  2.     struct llhead    *prev;
  3.     struct llhead    *next;
  4. };

  5. #define    LL_INIT(N)    ((N)->next = (N)->prev = (N))

  6. #define LL_HEAD(H)    struct llhead H = { &H, &H }

  7. #define LL_ENTRY(P,T,N) ((T *)((char *)(P) - offsetof(T, N)))

  8. #define    LL_ADD(H, N)                            \\
  9.     do {                                \\
  10.         ((H)->next)->prev = (N);                \\
  11.         (N)->next = ((H)->next);                \\
  12.         (N)->prev = (H);                    \\
  13.         (H)->next = (N);                    \\
  14.     } while (0)

  15. #define    LL_TAIL(H, N)                            \\
  16.     do {                                \\
  17.         ((H)->prev)->next = (N);                \\
  18.         (N)->prev = ((H)->prev);                \\
  19.         (N)->next = (H);                    \\
  20.         (H)->prev = (N);                    \\
  21.     } while (0)

  22. #define    LL_DEL(N)                            \\
  23.     do {                                \\
  24.         ((N)->next)->prev = ((N)->prev);            \\
  25.         ((N)->prev)->next = ((N)->next);            \\
  26.         LL_INIT(N);                        \\
  27.     } while (0)

  28. #define    LL_EMPTY(N)    ((N)->next == (N))

  29. #define    LL_FOREACH(H,N)    for (N = (H)->next; N != (H); N = (N)->next)

  30. #define LL_FOREACH_SAFE(H,N,T)                        \\
  31.     for (N = (H)->next, T = (N)->next; N != (H);            \\
  32.             N = (T), T = (N)->next)
应用例程: 
struct shttpd_ctx {
SSL_CTX *ssl_ctx; /* SSL context */
struct llhead registered_uris;/* User urls */
struct llhead error_handlers; /* Embedded error handlers */
struct llhead acl; /* Access control list */
struct llhead ssi_funcs; /* SSI callback functions */
struct llhead listeners; /* Listening sockets */
struct llhead workers; /* Worker workers */
FILE *access_log; /* Access log stream */
FILE *error_log; /* Error log stream */
char *options[NUM_OPTIONS]; /* Configurable options */
#if defined(__rtems__)
rtems_id         mutex;
#endif /* _WIN32 */
};

struct shttpd_ctx *ctx;

LL_INIT(&ctx->registered_uris);
LL_INIT(&ctx->error_handlers);



4.
5. 




阅读(1006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~