int urldecode(char * src,char * last,char * dest) [英] int urldecode(char *src, char *last, char *dest)
问题描述
这基于我在 http://www.cs .tut.fi / ~jkorpela /
表格/ cgic.html
#include< fcgi_stdio.h>
#include< stdlib.h>
int urldecode(char * src,char * last,char * dest){
int code;
for(; src!= last; src ++,dest ++){
if(* src ==''+'')* dest ='''';
else if(* src ==''%''){
if(sscanf(src + 1,"%2x",& code)!= 1 )code =''?'';
* dest =代码;
src + = 2;
}
else * dest = * src;
}
* dest =''\ n'';
* ++ dest = ''\''';
返回0;
}
int post(char * output){
char * lenstr = getenv(" CONTENT_LENGTH");
char * input;
long len;
if (lenstr!= NULL&& sscanf(le nstr,"%ld"& len)== 1){
fgets(输入,len + 1,stdin);
urldecode(输入,输入+ len,输出);
}
返回0;
}
int main(void ){
char *输出;
而(FCGI_Accept()> = 0){
post(输出);
printf(" Content-Type:text / xml; charset = utf-8"
" \\\\ nn"
" \\\\ nn"
" %s"
" \ n"
,输出);
}
返回0; < br $>
}
我做的唯一一件事是将它简化了一下,所以我能理解它更好一点。目标是从xmlhttprequest读取帖子,然后
返回其内容。
到目前为止我收到帖子但是urldecoding似乎没有播放
并输出任何建议?
This based on a example i found at http://www.cs.tut.fi/~jkorpela/
forms/cgic.html
#include <fcgi_stdio.h>
#include <stdlib.h>
int urldecode(char *src, char *last, char *dest){
int code;
for (; src != last; src++, dest++){
if (*src == ''+'') *dest = '' '';
else if(*src == ''%'') {
if(sscanf(src+1, "%2x", &code) != 1) code = ''?'';
*dest = code;
src +=2;
}
else *dest = *src;
}
*dest = ''\n'';
*++dest = ''\0'';
return 0;
}
int post(char *output){
char *lenstr=getenv("CONTENT_LENGTH");
char *input;
long len;
if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
fgets(input, len+1, stdin);
urldecode(input, input+len, output);
}
return 0;
}
int main (void){
char *output;
while (FCGI_Accept() >= 0) {
post(output);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"%s"
"\n"
,output);
}
return 0;
}
The only thing i did is simplified it a bit so i could understand it a
bit better. The goal is to read the post from a xmlhttprequest and
return its contents back.
So far i receive the post but the urldecoding seems not to play along
and scrambles the output, any suggestions ?
推荐答案
2月13日上午12:31, "格特" < gert.cuyk ... @ gmail.comwrote:
On Feb 13, 12:31 am, "gert" <gert.cuyk...@gmail.comwrote:
这是基于我发现的一个例子:http://www.cs.tut.fi/~jkorpela /
表格/ cgic.html
#include< fcgi_stdio.h>
#include< stdlib.h>
int urldecode(char * src,char * last,char * dest){
int code;
for(; src!= last; src ++,dest ++){
if(* src ==''+'')* dest ='''';
else if(* src ==''%''){
if(sscanf(src + 1,"%2x",& code)!= 1)code =''?'';
* dest =代码;
src + = 2;
}
else * dest = * src;
}
* dest =''\ n'';
* ++ dest =''\''';
返回0;
}
int post(char * output){
char * lenstr = getenv(" CONTENT_LENGTH");
char *输入;
长len;
if(lenstr!= NULL&& sscanf(lenstr,"%ld"& len)== 1){
fgets(输入,len + 1,stdin);
urldecode(输入,输入+ len,输出);
}
返回0;
}
int main(无效){
char *输出;
而( FCGI_Accept()> = 0){
post(输出);
printf(" Content-Type:text / xml; charset = utf-8"
" \\\\ nn"
" \\\\ nn"
" %s"
" \ n"
,输出);
}
返回0; < br $>
}
我做过的唯一一件事就是简化了一下,所以我能理解它好一点
更好一点。目标是从xmlhttprequest读取帖子,然后
返回其内容。
到目前为止我收到帖子但是urldecoding似乎没有播放
并输出输出,有什么建议吗?
This based on a example i found athttp://www.cs.tut.fi/~jkorpela/
forms/cgic.html
#include <fcgi_stdio.h>
#include <stdlib.h>
int urldecode(char *src, char *last, char *dest){
int code;
for (; src != last; src++, dest++){
if (*src == ''+'') *dest = '' '';
else if(*src == ''%'') {
if(sscanf(src+1, "%2x", &code) != 1) code = ''?'';
*dest = code;
src +=2;
}
else *dest = *src;
}
*dest = ''\n'';
*++dest = ''\0'';
return 0;
}
int post(char *output){
char *lenstr=getenv("CONTENT_LENGTH");
char *input;
long len;
if (lenstr != NULL && sscanf(lenstr, "%ld", &len) == 1) {
fgets(input, len+1, stdin);
urldecode(input, input+len, output);
}
return 0;
}
int main (void){
char *output;
while (FCGI_Accept() >= 0) {
post(output);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"%s"
"\n"
,output);
}
return 0;
}
The only thing i did is simplified it a bit so i could understand it a
bit better. The goal is to read the post from a xmlhttprequest and
return its contents back.
So far i receive the post but the urldecoding seems not to play along
and scrambles the output, any suggestions ?
我觉得这样做更好:)
if(lenstr!= NULL){
sscanf(lenstr,"%ld"& len)
fgets(输入,len + 1,stdin);
urldecode(输入,输入+ len,输出);
}
还有什么可靠的?
better do it like this i think :)
if (lenstr != NULL ) {
sscanf(lenstr, "%ld", &len)
fgets(input, len+1, stdin);
urldecode(input, input+len, output);
}
still scrambely doh ?
gert写道:
gert wrote:
这是基于我在 http://www.cs.tut.fi/~jkorpela/
表格/ cgic.html
This based on a example i found at http://www.cs.tut.fi/~jkorpela/
forms/cgic.html
< snip code>
<snip code>
>
我做过的唯一一件事就是简化它,所以我能理解它
好一点。目标是从xmlhttprequest读取帖子,然后
返回其内容。
到目前为止我收到帖子但是urldecoding似乎没有播放
并输出输出,任何建议?
>
The only thing i did is simplified it a bit so i could understand it a
bit better. The goal is to read the post from a xmlhttprequest and
return its contents back.
So far i receive the post but the urldecoding seems not to play along
and scrambles the output, any suggestions ?
编写一些测试以了解代码的工作原理。
-
Ian Collins。
Write some tests in order to understand how the code works.
--
Ian Collins.
gert写道:
gert wrote:
这基于我在 http://www.cs .tut.fi / ~jkorpela /
表格/ cgic.html
#include< fcgi_stdio.h>
#include< stdlib.h>
int urldecode(char * src,char * last,char * dest){
int code;
for(; src!= last; src ++,dest ++){
if(* src ==''+'')* dest ='''';
else if(* src ==''%''){
if(sscanf( src + 1,"%2x"& code)!= 1)code =''?'';
* dest = code;
src + = 2;
}
其他* dest = * src;
}
* dest =''\ n'';
* ++ dest =''\''';
返回0;
}
This based on a example i found at http://www.cs.tut.fi/~jkorpela/
forms/cgic.html
#include <fcgi_stdio.h>
#include <stdlib.h>
int urldecode(char *src, char *last, char *dest){
int code;
for (; src != last; src++, dest++){
if (*src == ''+'') *dest = '' '';
else if(*src == ''%'') {
if(sscanf(src+1, "%2x", &code) != 1) code = ''?'';
*dest = code;
src +=2;
}
else *dest = *src;
}
*dest = ''\n'';
*++dest = ''\0'';
return 0;
}
我写了一些类似于玩具cgi表格的东西,我做了一段时间后回来......
它不是很好的检查错误,但是我确实记得它至少工作。
#include< string.h>
#include< stdio.h>
#include< stdlib.h>
struct cgi_var__ {
char * key;
char * val;
size_t key_l;
size_t val_l;
};
static int form_decode(cgi_var * cv, size_t off)
{
char c,tmp [4];
int r;
if(cv-> val [off]!=''%''){
fprintf(stdout," form_decode :坏值\ n");
返回-1;
}
if((r =(cv-) > val + cv-> val_l) - (cv-> val + off))< 3){
fprintf(stdout,form_decode:length too short\ n);
返回-1;
}
memcpy(tmp,cv-> val + off,MIN(3,r)); tmp [3] =''\''';
c =(unsigned char)strtol(tmp + 1,NULL,16);
/ *
*数据==文字%7Cj%25%5C%25
* 01234567890123456
*
* c == |,off == 4,r == 13
* c ==%,off == 6,r == 9
* c == \\ \\,off == 7,r == 6
* c ==%,off == 8,r == 3
* /
memcpy(cv-> val + off,& c,sizeof c);
memmove(cv-> val + off + 1,cv-> val + off + 3,r - 3);
cv-> val_l - = 2;
r =(cv-> val + cv-> val_l) - (cv-> val + off);
返回0;
}
静态int cv_filter(cgi_var * cv)
{
size_t i;
/ *过滤掉rug-rats或解码%xx * /
for(i = 0; i< cv-> val_l; i ++){
if(cv-> val [i] =='' %'')
form_decode(cv,i);
switch(cv-> val [i]){
case'' +'':案例'' \\:壳体':壳体":
CV-> VAL [I] =‘’;休息;
默认:休息;
}
}
返回0;
}
I had written something similar for a toy cgi form util I did a while back...
It''s not super well checked for errors, but I do remember it atleast working.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct cgi_var__ {
char *key;
char *val;
size_t key_l;
size_t val_l;
};
static int form_decode(cgi_var *cv, size_t off)
{
char c, tmp[4];
int r;
if (cv->val[off] != ''%'') {
fprintf(stdout, "form_decode: bad value\n");
return -1;
}
if ((r = (cv->val + cv->val_l) - (cv->val + off)) < 3) {
fprintf(stdout, "form_decode: length too short\n");
return -1;
}
memcpy(tmp, cv->val + off, MIN(3, r)); tmp[3] = ''\0'';
c = (unsigned char)strtol(tmp + 1, NULL, 16);
/*
* data == text%7Cj%25%5C%25
* 01234567890123456
*
* c == |, off == 4, r == 13
* c == %, off == 6, r == 9
* c == \, off == 7, r == 6
* c == %, off == 8, r == 3
*/
memcpy(cv->val + off, &c, sizeof c);
memmove(cv->val + off + 1, cv->val + off + 3, r - 3);
cv->val_l -= 2;
r = (cv->val + cv->val_l) - (cv->val + off);
return 0;
}
static int cv_filter(cgi_var *cv)
{
size_t i;
/* filter out rug-rats or decode %xx */
for (i = 0; i < cv->val_l; i++) {
if (cv->val[i] == ''%'')
form_decode(cv, i);
switch (cv->val[i]) {
case ''+'': case ''\'''': case ''`'': case ''"'':
cv->val[i] = '' ''; break;
default: break;
}
}
return 0;
}
这篇关于int urldecode(char * src,char * last,char * dest)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!