Cgi koodinäiteid: erinevus redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
Resümee puudub |
Resümee puudub |
||
| 46. rida: | 46. rida: | ||
echo ' uus ok leht olemas ' | echo ' uus ok leht olemas ' | ||
echo $QUERY_STRING | echo $QUERY_STRING | ||
</pre> | |||
C näide, mis liidab kahte arvu (NB! csapp.h tuleb võtta tiny veebiserverist, vt kursuse lehel tiny.zip) | |||
<pre> | |||
/* | |||
* adder.c - a minimal CGI program that adds two numbers together | |||
*/ | |||
#include "csapp.h" | |||
int main(void) { | |||
char *buf, *p; | |||
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE]; | |||
int n1=0, n2=0; | |||
/* Extract the two arguments */ | |||
if ((buf = getenv("QUERY_STRING")) != NULL) { | |||
p = strchr(buf, '&'); | |||
*p = '\0'; | |||
strcpy(arg1, buf); | |||
strcpy(arg2, p+1); | |||
n1 = atoi(arg1); | |||
n2 = atoi(arg2); | |||
} | |||
/* Make the response body */ | |||
sprintf(content, "Welcome to add.com: "); | |||
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content); | |||
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>", | |||
content, n1, n2, n1 + n2); | |||
sprintf(content, "%sThanks for visiting!\r\n", content); | |||
/* Generate the HTTP response */ | |||
// printf("content-length: %d\r\n", strlen(content)); | |||
printf("content-type: text/html\n\n"); | |||
printf("%s", content); | |||
fflush(stdout); | |||
exit(0); | |||
} | |||
</pre> | </pre> | ||
Redaktsioon: 8. märts 2006, kell 10:28
Shelli skript, mis kuvab kataloogi listingu:
#!/bin/sh echo 'content-type: text/html' echo echo ' uus ok leht olemas ' ls -l
Pythoni skript, mis trükib ühe sõna kolm korda:
#!/usr/bin/python def foo(): print "<p>miskijutt</p>" print "content-type: text/html" print print "ok !! jalle olemas" foo() foo() foo()
C "hello world" programm, mis tuleks kompileerida.
#include <stdio.h>
int main(void) {
printf("content-type: text/html\n\n");
printf("Tere ise ka!\n");
/*
fflush(stdout);
exit(0);
*/
}
Shelli skript, mis kuvab etteantud cgi parameetrid:
#!/bin/sh echo 'content-type: text/html' echo echo ' uus ok leht olemas ' echo $QUERY_STRING
C näide, mis liidab kahte arvu (NB! csapp.h tuleb võtta tiny veebiserverist, vt kursuse lehel tiny.zip)
/*
* adder.c - a minimal CGI program that adds two numbers together
*/
#include "csapp.h"
int main(void) {
char *buf, *p;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
int n1=0, n2=0;
/* Extract the two arguments */
if ((buf = getenv("QUERY_STRING")) != NULL) {
p = strchr(buf, '&');
*p = '\0';
strcpy(arg1, buf);
strcpy(arg2, p+1);
n1 = atoi(arg1);
n2 = atoi(arg2);
}
/* Make the response body */
sprintf(content, "Welcome to add.com: ");
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>",
content, n1, n2, n1 + n2);
sprintf(content, "%sThanks for visiting!\r\n", content);
/* Generate the HTTP response */
// printf("content-length: %d\r\n", strlen(content));
printf("content-type: text/html\n\n");
printf("%s", content);
fflush(stdout);
exit(0);
}