大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> CGI专区 -> CGI教程(7)解码数据发送给CGI脚本之一

CGI教程(7)解码数据发送给CGI脚本之一

时间: 2021-07-31 作者:daque

解码数据发送给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 #endifparse::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_mapreturn file ? map_uname(mes) : mes;# elsereturn 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 '%' :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_mapif ( 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;}#endifreturn path;}#endif

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.