Introduction
Introduction Statistics Contact Development Disclaimer Help
Making CGI more obvious to use. - geomyidae - A small C-based gopherd.
git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfri…
Log
Files
Refs
Tags
README
LICENSE
---
commit 3912688b3929a44d7a43a99eca14178eb97ceff5
parent 8df91710dfdf1f9091be9ed40a259933c7b62951
Author: Christoph Lohmann <[email protected]>
Date: Mon, 21 Mar 2011 17:49:11 +0100
Making CGI more obvious to use.
Diffstat:
M CGI | 14 +++++++++++---
M handlr.c | 64 +++++++++--------------------…
M handlr.h | 10 +++++-----
M ind.h | 2 +-
M main.c | 19 +++++++++++++++----
5 files changed, 51 insertions(+), 58 deletions(-)
---
diff --git a/CGI b/CGI
@@ -23,20 +23,24 @@ CALLING CONVENTION
Geomyidae will call the script like this:
- % $gopherroot/test.cgi $search $arguments
+ % $gopherroot/test.cgi $search $arguments $host $port
When it is a plain request, the arguments will have these values:
C: /test.cgi
- -> $search = server host
+ -> $search = ""
-> $arguments = ""
+ -> $host = server host
+ -> $port = server port
If the request is for a type 7 search element, then the entered string by
the user will be seen as following:
C: /test.cgi searchterm (There is a Tab inbetwee…
-> $search = »searchterm«
- -> $arguments = server host
+ -> $arguments = ""
+ -> $host = server host
+ -> $port = server port
When you are trying to give your script some calling arguments, the syntax
is:
@@ -44,12 +48,16 @@ is:
C: /test.cgi?hello
-> $search = ""
-> $arguments = »hello«
+ -> $host = server host
+ -> $port = server port
If borth ways of input are combined, the variables are set as following:
C: /test.cgi?hello=world searchterm (Beware! A Tab!)
-> $search = »searchterm«
-> $arguments = »hello=world«
+ -> $host = server host
+ -> $port = server port
STANDARD CGI
diff --git a/handlr.c b/handlr.c
@@ -21,46 +21,33 @@
void
handledir(int sock, char *path, char *port, char *base, char *args,
- char *sear)
+ char *sear, char *ohost)
{
- char *pa, *file, *e, *addr, *par, *b;
+ char *pa, *file, *e, *par, *b;
struct dirent **dirent;
int ndir, i;
struct stat st;
filetype *type;
USED(sear);
- addr = nil;
pa = gstrdup(path);
e = strrchr(pa, '/');
if(e != nil) {
*e = '\0';
- if(args == nil) {
- addr = gmallocz(512, 2);
- if(gethostname(addr, 512) == -1) {
- perror("gethostname");
- free(addr);
- free(pa);
- return;
- }
- } else
- addr = gstrdup(args);
-
par = gstrdup(pa);
b = strrchr(par + strlen(base), '/');
if(b != nil) {
*b = '\0';
tprintf(sock, "1..\t%s\t%s\t%s\r\n",
- par + strlen(base), addr, port);
+ par + strlen(base), ohost, port);
}
free(par);
ndir = scandir(pa, &dirent, 0, alphasort);
if(ndir < 0) {
perror("scandir");
- free(addr);
free(pa);
return;
} else {
@@ -77,7 +64,7 @@ handledir(int sock, char *path, char *port, char *base, char …
type = gettype("index.gph");
e = file + strlen(base);
tprintf(sock, "%c%s\t%s\t%s\t%s\r\n", *type->t…
- dirent[i]->d_name, e, addr, port);
+ dirent[i]->d_name, e, ohost, port);
free(file);
free(dirent[i]);
}
@@ -86,18 +73,15 @@ handledir(int sock, char *path, char *port, char *base, cha…
tprintf(sock, "\r\n");
}
- if(addr != nil)
- free(addr);
free(pa);
}
void
handlegph(int sock, char *file, char *port, char *base, char *args,
- char *sear)
+ char *sear, char *ohost)
{
Indexs *act;
int i;
- char addr[512];
USED(base);
USED(args);
@@ -105,16 +89,8 @@ handlegph(int sock, char *file, char *port, char *base, cha…
act = scanfile(file);
if(act != nil) {
- if(args == nil) {
- if(gethostname(addr, sizeof(addr)) == -1) {
- perror("gethostname");
- return;
- }
- } else
- snprintf(addr, sizeof(addr), "%s", args);
-
for(i = 0; i < act->num; i++) {
- printelem(sock, act->n[i], addr, port);
+ printelem(sock, act->n[i], ohost, port);
freeelem(act->n[i]);
act->n[i] = nil;
}
@@ -126,7 +102,7 @@ handlegph(int sock, char *file, char *port, char *base, cha…
void
handlebin(int sock, char *file, char *port, char *base, char *args,
- char *sear)
+ char *sear, char *ohost)
{
char sendb[1024];
int len, fd;
@@ -136,6 +112,7 @@ handlebin(int sock, char *file, char *port, char *base, cha…
USED(base);
USED(args);
USED(sear);
+ USED(ohost);
fd = open(file, O_RDONLY);
if(fd >= 0) {
@@ -147,7 +124,7 @@ handlebin(int sock, char *file, char *port, char *base, cha…
void
handlecgi(int sock, char *file, char *port, char *base, char *args,
- char *sear)
+ char *sear, char *ohost)
{
char *p, *path;
@@ -169,6 +146,8 @@ handlecgi(int sock, char *file, char *port, char *base, cha…
if(sear == nil)
sear = "";
+ if(args == nil)
+ args = "";
dup2(sock, 0);
dup2(sock, 1);
@@ -177,7 +156,7 @@ handlecgi(int sock, char *file, char *port, char *base, cha…
case 0:
if (path != nil)
chdir(path);
- execl(file, p, sear, args, (char *)nil);
+ execl(file, p, sear, args, ohost, port, (char *)nil);
case -1:
break;
default:
@@ -192,9 +171,9 @@ handlecgi(int sock, char *file, char *port, char *base, cha…
void
handledcgi(int sock, char *file, char *port, char *base, char *args,
- char *sear)
+ char *sear, char *ohost)
{
- char *p, *path, *ln, addr[512];
+ char *p, *path, *ln;
int outpipe[2];
Elems *el;
@@ -216,16 +195,10 @@ handledcgi(int sock, char *file, char *port, char *base, …
if(p == nil)
p = file;
- if(args == nil) {
- if(gethostname(addr, sizeof(addr)) == -1) {
- perror("gethostname");
- return;
- }
- } else
- snprintf(addr, sizeof(addr), "%s", args);
-
if(sear == nil)
sear = "";
+ if(args == nil)
+ args = "";
dup2(sock, 0);
dup2(sock, 2);
@@ -235,7 +208,7 @@ handledcgi(int sock, char *file, char *port, char *base, ch…
close(outpipe[0]);
if (path != nil)
chdir(path);
- execl(file, p, sear, args, (char *)nil);
+ execl(file, p, sear, args, ohost, port, (char *)nil);
case -1:
break;
default:
@@ -247,7 +220,7 @@ handledcgi(int sock, char *file, char *port, char *base, ch…
if (el == nil)
continue;
- printelem(sock, el, addr, port);
+ printelem(sock, el, ohost, port);
freeelem(el);
}
tprintf(sock, "\r\n.\r\n\r\n");
@@ -260,3 +233,4 @@ handledcgi(int sock, char *file, char *port, char *base, ch…
break;
}
}
+
diff --git a/handlr.h b/handlr.h
@@ -7,14 +7,14 @@
#define HANDLR_H
void handledir(int sock, char *path, char *port, char *base, char *args,
- char *sear);
+ char *sear, char *ohost);
void handlegph(int sock, char *file, char *port, char *base, char *args,
- char *sear);
+ char *sear, char *ohost);
void handlebin(int sock, char *file, char *port, char *base, char *args,
- char *sear);
+ char *sear, char *ohost);
void handlecgi(int sock, char *file, char *port, char *base, char *args,
- char *sear);
+ char *sear, char *ohost);
void handledcgi(int sock, char *file, char *port, char *base, char *args,
- char *sear);
+ char *sear, char *ohost);
#endif
diff --git a/ind.h b/ind.h
@@ -27,7 +27,7 @@ typedef struct filetype filetype;
struct filetype {
char *end;
char *type;
- void (* f)(int, char *, char *, char *, char *, char *);
+ void (* f)(int, char *, char *, char *, char *, char *, char *);
};
filetype *gettype(char *filename);
diff --git a/main.c b/main.c
@@ -137,6 +137,7 @@ handlerequest(int sock, char *base, char *ohost, char *port…
bzero(&dir, sizeof(dir));
bzero(recvb, sizeof(recvb));
bzero(recvc, sizeof(recvc));
+ args = nil;
len = recv(sock, recvb, sizeof(recvb)-1, 0);
if(len > 0) {
@@ -164,8 +165,6 @@ handlerequest(int sock, char *base, char *ohost, char *port…
args = strchr(recvb, '?');
if(args != nil)
*args++ = '\0';
- else
- args = ohost;
securepath(recvb, len - 2);
snprintf(path, sizeof(path), "%s%s", base, recvb);
@@ -182,10 +181,10 @@ handlerequest(int sock, char *base, char *ohost, char *po…
if(c == nil)
c = path;
type = gettype(c);
- type->f(sock, path, port, base, args, sear);
+ type->f(sock, path, port, base, args, sear, ohost);
} else {
if(S_ISDIR(dir.st_mode)) {
- handledir(sock, path, port, base, args, sear);
+ handledir(sock, path, port, base, args, sear, ohost);
if(loglvl & DIRS)
logentry(clienth, clientp, recvc,
"dir listing");
@@ -307,6 +306,16 @@ main(int argc, char *argv[])
usage();
} ARGEND;
+ if(ohost == nil) {
+ ohost = gmallocz(513, 2);
+ if(gethostname(ohost, 512) < 0) {
+ perror("gethostname");
+ free(ohost);
+ return 1;
+ }
+ } else
+ ohost = gstrdup(ohost);
+
if(group != nil) {
if((gr = getgrnam(group)) == nil) {
perror("no such group");
@@ -419,6 +428,8 @@ main(int argc, char *argv[])
close(listfd);
if(logfile != nil)
stoplogging(glfd);
+ free(ohost);
+
return 0;
}
You are viewing proxied material from bitreich.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.