选择显示字体大小

cgi教程(7)解码数据发送给cgi脚本

解码数据发送给cgi脚本

  当使用表单的时候,收集在表单的信息给发送给cgi脚本用于处理。这个信息被放置在环境变量query_string中。

  为了清除地将信息传递给环境变量query_string,被修改锚标签的表单将被使用。在这个被修改的锚标签中,传递给环境变量query_string的数据是在指示cgi脚本的url之后附上的。字符”?”被用来分隔指定cgi脚本以及发送给脚本的数据的url。比如:

<a href="/cgi-bin/script?name=your+name&action=find"> link </a>

其中数据"name=your+name&action=find"被放置在环境变量query_string中并且cgi脚本被执行。

  下面给出一个例子:由c++编写一个类,具体文件parse.h 和parse.cpp被用于在query_string中提取个别的组件,其中的头文件t99_type.h在上节教程已经提到,它是包含了一些定义。具体代码如下:

//以下是parse.h文件

#ifndef class_parse

#define class_parse

//#define no_map // 定义没有用户处理

#include "t99_type.h"//这个文件在前面教程中有

class parse

{

public:

parse( char [] );

~parse();

void set( char [] );

char *get_item( char [], int pos=1, bool=false );

char *get_item_n( char [], int pos=1, bool=false );

protected:

void remove_escape(char []);

int hex( char ); //返回十六进制数

char *map_uname( char [] );

private:

enum { sep = '&' }; // 使用&分隔字符

char *the_str; // 字符部分

int the_length; // 字符长度

};

#endif

//以下是parse.cpp文件

#ifndef class_parse_imp

#define class_parse_imp

#include "parse.h"

#include

#include

#ifndef no_map

# include

#endif

parse::parse( char list[] )

{

the_str = null;

set( list );

}

parse::~parse()

{

if ( the_str != null ) { // 释放存储器

delete [] the_str;

}

}

void parse::set( char list[] )

{

if ( the_str != null ) { // 释放存储器

delete [] the_str;

}

the_length = strlen( list ); // 字符长度

the_str = new char[the_length+1]; // 分配空间

strcpy( the_str, list ); // 复制

}

char *parse::get_item( char name[], int pos, bool file )

{

int len = strlen( name );

int cur_tag = 1;

for( int i=0; i
{

if ( the_str[i] == name[0] &&

strncmp( &the_str[i], name, len ) == 0 )

{

if ( the_str[i+len] == '=' )

{

if ( cur_tag == pos )

{

int start = i+len+1; int j = start;

while ( the_str[j] != sep && the_str[j] != '\0' ) j++;

int str_len = j-start;

char *mes = new char[ str_len+1 ];

strncpy( mes, &the_str[start], str_len );

mes[str_len] = '\0';

remove_escape( mes );

# ifndef no_map

return file ? map_uname(mes) : mes;

# else

return file ? mes : mes;

# endif

} else {

cur_tag++;

}

}

}

}

return null;

}

char *parse::get_item_n( char name[], int pos, bool file )

{

char *res = get_item( name, pos, file );

return res == null ? (char*)"----" : res;

}

void parse::remove_escape(char str[])

{

char * from = &str[0];

char * to = &str[0];

while ( *from != '\0' )

{

char ch = *from++;

switch ( ch )

{

case '&#37;' :

ch = (hex(*from++)><4) hex(*from++);

break;

case '+' :

ch = ' '; break;

}

*to++ = ch;

}

*to = '\0';

}

int parse::hex( char c )

{

if ( isdigit( c ) )

return c-'0';

if ( isalpha( c ) )

return tolower(c)-'a'+10;

return 0;

}

char* parse::map_uname( char *path )

{

#ifndef no_map

if ( path[0] == '~' )

{

char uname[255]; // 容纳用户名字的变量

char *rest = &path[1];

char *p = &uname[0];

while ( *rest != '/' && *rest != '\0' )

{

*p++ = *rest++;

}

*p = '\0'; // 结束标识

char *root = uname;

passwd *pw = getpwnam( uname );

if ( pw != null )

{

root = pw->pw_dir; // 用户的主目录

}

int len_root = strlen(root);

int len_path = len_root + strlen(rest);

char *new_path = new char[len_path+1]; // 动态字符

strcpy( &new_path[0], root ); // 复制用户路径

strcpy( &new_path[len_root], rest ); // 其余部分

return new_path;

} else {

return path;

}

#endif

return path;

}

#endif


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons