Last active
February 20, 2016 12:20
-
-
Save appkr/ef88906dc7c0c37e7c5d to your computer and use it in GitHub Desktop.
old school
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub cgiReadInput { | |
if ($ENV{'REQUEST_METHOD'} eq 'POST') { | |
read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); | |
} | |
if ($ENV{'REQUEST_METHOD'} eq 'GET') { | |
$query_string = $ENV{'QUERY_STRING'}; | |
} | |
@pairs = split(/&/, $query_string); | |
foreach $field(@pairs) { | |
($name, $value) = split(/=/, $field); | |
$name = &strURLDecode($name); | |
$value = &strURLDecode($value); | |
$FORM{$name} = $value; | |
} | |
$cookie_string = $ENV{'HTTP_COOKIE'}; | |
@pairs = split(/; /, $cookie_string); | |
foreach $field(@pairs) { | |
($name, $value) = split(/=/, $field); | |
$name = &strURLDecode($name); | |
$value = &strURLDecode($value); | |
$COOKIE{$name} = $value; | |
} | |
} | |
sub strURLEncode { | |
$str = ''; | |
for ($i = 0; $i < length($_[0]); $i++) { | |
$tok = substr($_[0], $i, 1); | |
if ($tok =~ /[ 0-9A-Za-z@.]/) { | |
if ($tok =~ / /) { | |
$str .= "+"; | |
} else { | |
$str .= $tok; | |
} | |
} else { | |
$str .= "%"; | |
$str .= sprintf("%02X", ord($tok)); | |
} | |
} | |
$str; | |
} | |
sub strURLDecode { | |
$str = $_[0]; | |
$str =~ tr/+/ /; | |
$str =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/egi; | |
$str; | |
} | |
sub cgiSetCookie { | |
$name = $_[0]; | |
$value = $_[1]; | |
$name = &strURLEncode($name); | |
$value = &strURLEncode($value); | |
print "Set-Cookie: $name=$value;\n"; | |
} | |
return 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ gcc write.c cgic.c -o write.cgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub parse_arguments { | |
local($pair, $name, $value, $content_type, $content_length, $buffer, $dump, $boundary, $line, $array_value, $i); | |
local(@pairs, @column); | |
if ($ENV{'QUERY_STRING'}) { | |
@pairs = split(/&/, $ENV{'QUERY_STRING'}); | |
foreach $pair (@pairs) { | |
($name, $value) = split(/=/, $pair); | |
$value =~ tr/+/ /; | |
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; | |
if ($name eq 'head' || $name eq 'foot') { | |
$value =~ s/\n/\\n/g ; | |
$value =~ s/\n/\\n/g ; | |
} elsif ($name ne 'profile') { | |
$value =~ s/\n//g ; | |
} | |
$FORM{$name} = $value; | |
} | |
} else { | |
$content_type = $ENV{'CONTENT_TYPE'}; | |
$content_length = $ENV{'CONTENT_LENGTH'}; | |
binmode STDIN; | |
read(STDIN,$buffer,$content_length); | |
if ((!$content_type) || ($content_type eq 'application/x-www-form-urlencoded')) { | |
@pairs = split(/&/, $buffer); | |
foreach $pair (@pairs) { | |
($name, $value) = split(/=/,$pair); | |
$value =~ tr/+/ /; | |
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; | |
$value =~ s/\n//g unless ($name eq 'body'); | |
$FORM{$name} = $value; | |
} | |
} elsif ($content_type =~ m#^multipart/form-data#) { | |
($boundary = $content_type) =~ s/^.*boundary=(.*)$/\1/; | |
@pairs = split(/--$boundary/, $buffer); | |
@pairs = splice(@pairs,1,$#pairs-1); | |
$i = 0; | |
for $pair (@pairs) { | |
($dump,$line,$value) = split(/\r\n/,$pair,3); | |
if ($line =~ /filename/) { | |
$gRealFile[$i++] = $line; | |
} | |
next if $line =~ /filename=\"\"/; | |
$line =~ s/^Content-Disposition: form-data; //; | |
(@column) = split(/;\s+/, $line); | |
($name = $column[0]) =~ s/^name="([^"]+)"$/\1/g; | |
if ($#column > 0) { | |
if($value =~ /^Content-Type:/) { | |
($dump,$dump,$value) = split(/\r\n/,$value,3); | |
} else { | |
($dump,$value) = split(/\r\n/,$value,2); | |
} | |
} else { | |
($dump,$value) = split(/\r\n/,$value,2); | |
if (grep(/^$name$/, keys(%CGI))) { | |
if (@{$FORM{$name}} > 0) { | |
push(@{$FORM{$name}}, $value); | |
} else { | |
$array_value = $FORM{$name}; | |
undef $FORM{$name}; | |
$FORM{$name}[0] = $array_value; | |
push(@{$FORM{$name}}, $value); | |
} | |
} else { | |
next if $value =~ /^\s*$/; | |
$FORM{$name} = $value; | |
chop($FORM{$name}); | |
chop($FORM{$name}); | |
} | |
next; | |
} | |
$FORM{$name} = $value; | |
} | |
} else { | |
print "Invalid content type!\n"; | |
exit(1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cgic.h" | |
#define MAX_LEN 255 | |
int str_check(char *str) { | |
if (str === NULL) { | |
return 0; | |
} | |
if (strlen(str) === 0) { | |
return 0; | |
} | |
return 1; | |
} | |
void cgi_error_message(char *msg) { | |
cgiHeaderContentType("text/plain"); | |
printf("%s", msg); | |
exit(1); | |
} | |
int cgi_main(void) { | |
char name[MAX_LEN], title[MAX_LEN], doc[MAX_LEN], email[MAX_LEN], homepage[MAX_LEN]; | |
cgiFormString("writer", name, MAX_LEN); | |
cgiFormString("title", title, MAX_LEN); | |
cgiFormString("doc", doc, MAX_LEN); | |
cgiFormString("email", email, MAX_LEN); | |
cgiFormString("homepage", homepage, MAX_LEN); | |
if (! str_check(name)) { | |
cgi_error_message("Please provide you name"); | |
} | |
if (! str_check(title)) { | |
cgi_error_message("Please provide a title"); | |
} | |
if (! str_check(doc)) { | |
cgi_error_message("Please provide post contents"); | |
} | |
cgiHeaderContentType("text/html"); | |
printf("<html>\n"); | |
printf("<head><title>GuestBook</title></head>\n"); | |
printf("<body>\n"); | |
printf("<h2>GuestBook</h2>\n"); | |
printf("<hr>\n"); | |
printf("<p>\n"); | |
if (str_check(email)) { | |
printf("<a href=\"mailto:%s\">%s</a>\n", email, name); | |
} else { | |
printf("%s\n", name); | |
} | |
if (str_check(homepage)) { | |
printf("<small><a href=\"%s\">%s</small>\n", homepage, homepage); | |
} | |
printf("</p>\n"); | |
printf("<p>What you wrote is:</p>\n") | |
printf("<table border=\"1\">\n"); | |
printf("<tr>\n"); | |
printf("<td><b>Title</b> : %s</td>\n", title); | |
printf("</tr>\n"); | |
printf("<tr>\n"); | |
printf("<td>%s</td>\n", doc); | |
printf("</tr>\n"); | |
printf("</table>\n"); | |
printf("<p>Your post has been saved !</p>\n"); | |
printf("</body>\n"); | |
printf("</html>"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment