利用cgic库怎么把从应用程序中的值返回给网页

利用cgic库怎么把从应用程序中的值返回给网页,第1张

修改Makefile文件:

找到CC=gcc,将其改成CC=arm-linux-gcc,

找到AR=ar,将其改成AR=arm-linux-ar,

找到RANLIB=ranlib,将其改成RANLIB=arm-linux-ranlib。

找到gcc cgictest.o -o cgictest.cgi ${LIBS},将其改成$(CC) $(CFLAGS) cgictest.o -o cgictest.cgi ${LIBS},

找到gcc capture.o -o capture ${LIBS},将其改成$(CC) $(CFLAGS) capture.o -o capture ${LIBS},并保存森基退出。1234512345

然后运行make进行编译,编译后得到的CGIC库libcgic.a(该库在需要编写cgi接芹春姿口程序时调用使用嫌绝)以及CGI库的测试程序capture与cgictest.cgi,这两个测试程序仅用来演示CGIC库如何使用,为此,将capture和cgictest.cgi拷贝到目标主机的/var/www/cgi-bin目录下。

用C语言编写cgi程序的话,CGIC是非常流行的库,官方页面及下载地址为: www.boutell.com/cgic/#obtain 不少网站都有文件上传的功能,本文展示如何用CGIC库编写文件上传的服务端程序,最后给出一段简单的HTML代码,供大家测试使用。//upload.c#include #include #include #include #include #include"cgic.h"#define BufferLen 1024int cgiMain(void){ cgiFilePtr fileint targetFilemode_t modechar name[128]char fileNameOnServer[64]char contentType[1024]char buffer[BufferLen]char *tmpStr=NULLint sizeint got,tcgiHeaderContentType("text/html")//神燃取得html页面中file元素的值,应该是文件在客户机上的路径名 if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) { fprintf(stderr,"could not retrieve filename\n")goto FAIL} cgiFormFileSize("file", &size)//取得文件类型,不过本例中并未使用 cgiFormFileContentType("file", contentType, sizeof(contentType))//目前文件存在于系统临时文件夹中搭瞎野,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件 if (cgiFormFileOpen("file", &file) != cgiFormSuccess) { fprintf(stderr,"could not open the file\n")goto FAIL} t=-1//从路径名解析出用户文件名 while(1){ tmpStr=strstr(name+t+1,"\\")if(NULL==tmpStr) tmpStr=strstr(name+t+1,"/")//if "\\" is not path separator, try "/" if(NULL!=tmpStr) t=(int)(tmpStr-name)else break} strcpy(fileNameOnServer,name+t+1)mode=S_IRWXU|S_IRGRP|S_IROTH//在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件 targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode)if(targetFile<0){ fprintf(stderr,"could not create the new file,%s\n",fileNameOnServer)goto FAIL} //从系统临时文件中读出文件内容,并放到刚创建的目标文件中 while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){ if(got>0) write(targetFile,buffer,got)} cgiFormFileClose(file)close(targetFile)goto ENDFAIL: fprintf(stderr,"Failed to upload")return 1END: printf("File \"%s\" has been uploaded",fileNameOnServer)return 0} 假设该文件存储为upload.c,则使用如下命令编辑:gcc -Wall upload.c cgic.c -o upload.cgi 编译完成后把upload.cgi复制到你部署cgi程序知喊的目录(通常命名为cgi-bin)。 正式部署时,请务必修改用open创建新文件那一行代码。把open的第一个参数设置为目标文件在服务器上存储的绝对路径,或者相对于cgi程序的相对路径。本例中,出于简单考虑,在cgi程序所在目录下创建新文件。

CGIC介绍

怎样写CGIC应用程序

怎样产生图片在CGIC中?

CGI调试特征: 利用捕获

cgic函数参考

cgic变量参考

cgic结果编码参考

cgic快速索引

一般的UNIX系统都支持ANSIC,增加相应的库函数(和相应的h文件)就可以实现CGI。在此我向大家推荐一个用于CGI编程的ANSIC库:cgic。

cgic是用来生成基于CGI的WWW应用程序的C语言函数库,它有以下功能:

*对数据进行语法分析

*接收以GET和PSOT两种方式发送的数据

*把FORM中的不同域连接成连续的串

*为检索FORM数据而提供字符串,整数,浮点以及单项和多项选择功能

*为数字字段提供边界检测

*把CGI环境变量加载到非空的C串中

*为调试而捕捉CGI状态

*提供相对安全的系统调用功能

用一般ANSI C或C++编译器就可以编译cgic程序,不过与通常C程序不同的是,用cgic写的源码其主函数是cgiMain(),而不是通常的main()。cgic的函数库会自动把cgiMain连接到相应的main()上去。

--------------------------------------------------------------------------------

写CGIC程序

Note: 所有的cgic应用程序必须连接cgic.c.

用cgimain()替代main() 必须包含: #include"cgic.h."

基本结构cgictest.c:

int cgiMain() {

#if DEBUG

/* Load a saved CGI scenario if we're debugging */

cgiReadEnvironment("/path/to/capcgi.dat")

#endif

/* Important: we must indicate the type of document */

cgiHeaderContentType("text/html")

/* Now invoke other functions to handle each part of the form */

fprintf(cgiOut, "<HTML><HEAD>\芹卖乱n")

fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n"):

fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n")

Name()

Address()

Hungry()

Temperature()

Frogs()

Color()

Flavors()

NonExButtons()

RadioButtons()

fprintf(cgiOut, "</BODY></HTML>\n")

/* This value will be the exit code of the program0

generally indicates success among Unix and DOS programs */

return 0

}

capture

输出标头

cgiHeaderContentType()在输出文挡之前简要说明MIME内配昌型,如 "text/html"。

cgiHeaderStatus()代替输出错误代码 cgiHeaderLocation()代替重新引导至其他页面。在一个独立的应用程序中只能有一个cgiHeader函数。

重点:在cgiHeader函数组中, cgiHeaderContentType(), 在任何向浏览器输出之前被调用. 否则将出错或浏览器不能识别。 cgiOut

接着, cgiMain() 调用不同的函数.当函数结束后,将返回0

处理输入文本

void Name() {

char name[81]

cgiFormStringNoNewlines("name", name, 81)

fprintf(cgiOut, "Name: %s<BR>\n", name)

}

这个函数的功能就是取的并显示由用户输入的name .

处理输出

Important: cgiOut通常相当于stdout

cgiFormString 确保断航

处理单一Checkboxes输入嫌档

这个Hungry() function确定用户是否选择"hungry"这个 checkbox:

void Hungry() {

if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {

fprintf(cgiOut, "I'm Hungry!<BR>\n")

} else {

fprintf(cgiOut, "I'm Not Hungry!<BR>\n")

}

}

这个函数依靠 cgiFormCheckboxSingle() 确定单一的checkbox 被选择。 cgiFormCheckboxSingle() 接受checkbox名字的属性值,如果存在就返回 cgiFormSuccess,否则返回cgiFormNotFound 如果是多项checkboxes,就用 cgiFormCheckboxMultiple()和cgiFormStringMultiple() 函数.

处理数字输入

Temperature() 返回浮点书的值确保在特定的返回内。

void Temperature() {

double temperature

cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6)

fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature)

}

依靠cgiFormDoubleBounded()得到数据.第一个数据是返回数据中输入域的名字。最后一个值是用户没有提交时的默认值。

这个函数总是找回在特定返回内合适的值cgiFormDoubleBounded返回的值被检查确信用户输入的资料在规定范围内, 而不是其他无效的数据。查看 cgiFormDoubleBounded() 更多的资料. 如果限度检查不理想,可以用 cgiFormDouble() 替代.

在整数输入,cgiFormInteger 和 cgiFormIntegerBounded 可以利用. 这些函数的功能类似.

处理单一选择输入

<SELECT>HTML标签被用于向用户提供几个选择. Radio buttons 和checkboxes 椰油这样的用途,大门、能够选择的数量很小时. Color()

char *colors[] = {

"Red",

"Green",

"Blue"

}

void Color() {

int colorChoice

cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0)

fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice])

}

这个函数确定用户选择了几个选项从<SELECT>在表但的列表. cgiFormSelectSingle()

cgiFormSelectSingle() 总是显示合理的选项值.

radio button也可以用这个函数.另外还有 cgiFormRadio(), 也是一样的

处理多项选择的输入

NonExButtons()

char *votes[] = {

"A",

"B",

"C",

"D"

}

void NonExButtons() {

int voteChoices[4]

int i

int result

int invalid

char **responses

/* Method #1: check for valid votes. This is a good idea,

since votes for nonexistent candidates should probably

be discounted... */

fprintf(cgiOut, "Votes (method 1):<BR>\n")

result = cgiFormCheckboxMultiple("vote", votes, 4,

voteChoices, &invalid)

if (result == cgiFormNotFound) {

fprintf(cgiOut, "I hate them all!<p>\n")

} else {

fprintf(cgiOut, "My preferred candidates are:\n")

fprintf(cgiOut, "<ul>\n")

for (i=0(i <4)i++) {

if (voteChoices[i]) {

fprintf(cgiOut, "<li>%s\n", votes[i])

}

}

fprintf(cgiOut, "</ul>\n")

}

参考cgiFormCheckboxMultiple(), cgiFormSelectMultiple().

cgiFormCheckboxMultiple() cgiFormCheckboxMultiple

NonExButtons() 函数在 cgictest.c:

/* Method #2: get all the names voted for and trust them.

This is good if the form will change more often

than the code and invented responses are not a danger

or can be checked in some other way. */

fprintf(cgiOut, "Votes (method 2):<BR>\n")

result = cgiFormStringMultiple("vote", &responses)

if (result == cgiFormNotFound) {

fprintf(cgiOut, "I hate them all!<p>\n")

} else {

int i = 0

fprintf(cgiOut, "My preferred candidates are:\n")

fprintf(cgiOut, "<ul>\n")

while (responses[i]) {

fprintf(cgiOut, "<li>%s\n", responses[i])

i++

}

fprintf(cgiOut, "</ul>\n")

}

/* We must be sure to free the string array or a memory

leak will occur. Simply calling free() would free

the array but not the individual strings. The

function cgiStringArrayFree() does the job completely. */

cgiStringArrayFree(responses)

}

参考cgiFormStringMultiple()

cgiFormStringMultiple()

/* An array of stringseach C string is an array of characters */

char **responses

cgiFormStringMultiple("vote", &responses)

检查CGI环境变量

将用到的变量 这里,

产生图象

#include "cgic.h"

#include "gd.h"

char *colors[] = {

"red", "green", "blue"

}

#define colorsTotal 3

int cgiMain() {

int colorChosen

gdImagePtr im

int r, g, b

/* Use gd to create an image */

im = gdImageCreate(64, 64)

r = gdImageColorAllocate(im, 255, 0, 0)

g = gdImageColorAllocate(im, 0, 255, 0)

b = gdImageColorAllocate(im, 0, 0, 255)

/* Now use cgic to find out what color the user requested */

cgiFormSelectSingle("color", 3, &colorChosen, 0)

/* Now fill with the desired color */

switch(colorChosen) {

case 0:

gdImageFill(im, 32, 32, r)

break

case 1:

gdImageFill(im, 32, 32, g)

break

case 2:

gdImageFill(im, 32, 32, b)

break

}

/* Now output the image. Note the content type! */

cgiHeaderContentType("image/gif")

/* Send the image to cgiOut */

gdImageGif(im, cgiOut)

/* Free the gd image */

gdImageDestroy(im)

return 0

}

为调试而捕捉CGI状态

cgic函数参考

cgiFormResultType cgiFormString( char *name, char *result, int max)

用于从输入域中copy字符串。他将域名max-1字节中的字符copy到缓冲区result。若域不存在,则copy一个空串到result缓冲区。在此函数中所有的新行由换行符代表。

cgiFormResultType cgiFormStringNoNewlines( char *name, char *result, int max)

它与cgiFormString函数相似,只是所有的CR和LF都被去掉了。

cgiFormResultType cgiFormStringSpaceNeeded( char *name, int *length)

它返回指向name的字符串的长度,并将长度放入length中。

cgiFormResultType cgiFormStringMultiple( char *name, char ***ptrToStringArray)

若同一名字有多个输入域,或域中的字符串可以动态变化,那么你可以使用本函数。它把名为name的所有输入域的值放在prtToStringArray中。

void cgiStringArrayFree(char **stringArray)

它释放了分配给stringArray的内存。

cgiFormResultType cgiFormInteger( char *name, int *result, int defaultV)

从输入域中取出整数放入result中。

cgiFormResultType cgiFormIntegerBounded( char *name, int *result, int min, int max, int defaultV)

若输入域中的整数在界限内则取出并放入result中。

cgiFormResultType cgiFormDouble( char *name, double *result, double defaultV)

从输入域中取出浮点数放入result中。

cgiFormResultType cgiFormDoubleBounded( char *name, double *result, double min, double max, double defaultV)

若输入域中的浮点数在界限内则取出并放入result中。

cgiFormResultType cgiFormSelectSingle( char *name, char **choicesText, int choicesTotal, int *result, int defaultV)

取出复选框(跟在select语句之后的),把选择的名字copy到choicesText,把选择的个数copy到choicesTotal,把当前的选择copy到result。

cgiFormResultType cgiFormSelectMultiple( char *name, char **choicesText, int choicesTotal, int *result, int *invalid)

与cgiFormSelectSingle类似,只指向整型数组的result代表了选择的项。

cgiFormResultType cgiFormCheckboxSingle( char *name)

若复选框被选中,则函数返回cgiFormSuccess,否则返回cgiFormNotFound。

cgiFormResultType cgiFormCheckboxMultiple( char *name, char **valuesText, int valuesTotal, int *result, int *invalid)

与cgiFormCheckboxSingle类似,但它处理同一名字有多个复选框的情况。name指向复选框的名字;valuesText指向包含有每个复选框中参数的一个数组;valuesTotal指向复选框的总数;result是一个整型数组,每个复选框选中的用1代表,没选中的用0代表。

cgiFormResultType cgiFormRadio( char *name, char **valuesText, int valuesTotal, int *result, int defaultV)

与cgiFormCheckboxMultiple相似,只是这里是单选按钮而不是复选框。

void cgiHeaderLocation(char *redirectUrl)

重定向到redirectUrl指定的URL。

void cgiHeaderStatus(int status, char *statusMessage)

输出状态代码status和消息statusMessage。

void cgiHeaderContentType(char *mimeType)

用于告知浏览器返回的是什么类型的文档。

cgiEnvironmentResultType cgiWriteEnvironment(char *filename)

本函数把当前CGI环境写入filename文件中以便以后调试时使用

cgiEnvironmentResultType cgiReadEnvironment(char *filename)

本函数从filename文件中读取CGI环境以便用来调试。

int cgiMain()

一个程序必须要写这个函数, 这是主程序开始之处。

cgic变量参考

This section provides a reference guide to the various global variables provided by cgic for the programmer to utilize. These variables should always be used in preference to stdin, stdout, and calls to getenv() in order to ensure compatibility with the cgic CGI debugging features.

大多数的变量相当于各种CGI变量,重要的是VGIC的变量不能为空.

char *cgiServerSoftware

服务器软件名称,或者一个空的字符串 or to an empty string if unknown.

char *cgiServerName

返回服务器名称或空

char *cgiGatewayInterface

网关接口 (通常是 CGI/1.1),或空

char *cgiServerProtocol

网络协议(usually HTTP/1.0),或空

char *cgiServerPort

服务器端口(usually 80),或空

char *cgiRequestMethod

请求方式 (usually GET or POST),或空

char *cgiPathInfo

指出附加虚拟路径

char *cgiPathTranslated

指出附加虚拟路径并由服务器转为本地路径

char *cgiScriptName

调用程序的名字

char *cgiQueryString

包含 GET-method 请求或者 <ISINDEX>标签. 这个信息不需要解吸,除非用<ISINDEX>标签通常由CGIC函数库自动解析。

char *cgiRemoteHost

从浏览器返回客户主机的名字

char *cgiRemoteAddr

从浏览器返回客户的IP地址

char *cgiAuthType

返回用户授权信息

char *cgiRemoteUser

鉴别用户 cgiAuthType.

char *cgiRemoteIdent

返回用户的名字(用户通过用户坚定协议)这个消息是不安全的,特别是Windows系统。

char *cgiContentType

返回MIME内型

char *cgiAccept

参考 cgiHeaderContentType() cgiUserAgent

char *cgiUserAgent

取的用户浏览器信息

char *cgiReferrer

指向用户访问的URL.

int cgiContentLength

表单或查询数据的字节被认为是标准的.

FILE *cgiOut

CGI输出. cgiHeader函数,象cgiHeaderContentType, 首先被用于输出mime头用于 fprintf() 和fwrite(). cgiOut通常相当于stdout。

FILE *cgiIn

CGI输入. 在决大部分时间你都不会需要这个函数。

cgic结果编码参考

在大量的按列中, cgic函数有计划的产生合理的结果,甚至浏览器和用户不合理时。无论如何, 有时候知道不合理的事情发生,尤其赋予一个值或定义一个范围是一个不充分的解决方案。下面的这些结果编码有助更好了解。

cgiFormSuccess

提交信息成功

cgiFormTruncated

删除部分字节.

cgiFormBadType

错误的输入信息(没有按要求)

cgiFormEmpty

提交信息为空.

cgiFormNotFound

提交信息没有找到.

cgiFormConstrained

数字属于某个特定的范围,被迫低于或高于适当范围。

cgiFormNoSuchChoice

单一选择提交的值是不被接受。通常说明表但和程序之间存在矛盾。

cgiEnvironmentIO

从CGI环境或获取的文件读或写的企图失败,报出I/O的错误。

cgiEnvironmentMemory

从CGI环境或获取的文件读或写的企图失败,报出out-of-memory的错误。

cgiEnvironmentSuccess

从CGI环境或获取的文件读或写的企图成功。

cgic快速索引


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/8153116.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存