|
Home > Archive > Unix Programming > December 2006 > Implementing a http server
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Implementing a http server
|
|
| Peyman Taher 2006-12-21, 1:29 am |
| I'm trying to implement a small http server to learn unix programming,
and there're few problems i'm trying to solve. I appreciate if you can
help.
suppose I have a PERL cgi source file, called "test.pl", and the server
recieves a request for that file, what i do is this:
each time i set the "chmod +x test.pl" using the chmod syscall, then i
putenv("QUERY_STRING="" "" ") and then execl("test.pl", "test.pl",
NULL).
first of all, what is the best way to do so? or how usually those web
servers that actually work, handle this?
then, now i need to redirect the output of the PERL program which goes
to STDOUT to the socket, and send it over the socket file descriptor,
how can i do that? is it done with popoen() or dup2()? which is better?
and another thing is that, if browser send some parameters (after ?
sign in request), then what i do is that i set the QUERY_STRING to that
value, BUT, PERL function param() which is supposed to parse
QUERY_STRING and retrive data, seems to not working for me. i set
QUERY_STRING envv and call the execl() to execute my PERL source file,
then in the PERL file i have this:
print "$ENV{QUERY_STRING}\n";
if (param) {
print "found a parameter";
}
the first line, does print the value i set for envv, meaning that the
envv pointer is passed to the program, but the param() thing, returns
false, meaning that there's no QUERY_STRING!!! why? what might be
wrong? I read the cgi-rfc, and it says that the only thing i need is to
set QUERY_STRING = query_string; and query_string = *somestring; and
somestring = reserved | unreserved | ..., which is exactly what i do!
| |
| Hubble 2006-12-21, 7:31 am |
| Peyman Taher schrieb:
> I'm trying to implement a small http server to learn unix programming,
> and there're few problems i'm trying to solve. I appreciate if you can
> help.
>
> suppose I have a PERL cgi source file, called "test.pl", and the server
> recieves a request for that file, what i do is this:
>
> each time i set the "chmod +x test.pl" using the chmod syscall, then i
> putenv("QUERY_STRING="" "" ") and then execl("test.pl", "test.pl",
> NULL).
> first of all, what is the best way to do so? or how usually those web
> servers that actually work, handle this?
> then, now i need to redirect the output of the PERL program which goes
> to STDOUT to the socket, ...
First, the httpd should not use chmod +x. This is the job of the
administrator and/or setup program and needs only be done once.
IMHO, better use execle or execve and not use putenv. If you use
putenv, consider setenv, since it does not need parsing.
After fork(), use dup2 to copy the accepted file discriptor to
stdin/stdout.
outline:
fd=accept(...)
if (pid=fork())==0) {
dup2(fd,0);
dup2(fd,1);
... setup vectors for execve
execve(...);
}
Notes: Remember to check the return values of all system calls for
errors. Needs a SIGCHLD signal handler to remove zombie processes.
After trying yourself, have a look at existing small httpd
implementations: For instance
http://www.acme.com/software/mini_httpd/
http://www.acme.com/software/micro_httpd/
Hubble
| |
| ilko.k.v@gmail.com 2006-12-21, 7:31 am |
| You are need reading www.kegel.com
| |
| Peyman Taher 2006-12-23, 1:35 am |
|
On Dec 21, 5:01 pm, "Hubble" <rei...@huober.de> wrote:
> This is the job of the
> administrator and/or setup program and needs only be done once.
> ...
Thank you so much.
| |
|
| Hubble wrote:
> Peyman Taher schrieb:
>
> After trying yourself, have a look at existing small httpd
> implementations: For instance
>
> http://www.acme.com/software/mini_httpd/
> http://www.acme.com/software/micro_httpd/
Here's a longer list, for posterity. Mainly these are selected because
they 1) are stable/mature 2) have simplicity and small footprint as
design goals. There are hundreds of other web servers out there that
don't have those two attributes. But I've probably missed some, and
some of those I've listed may not be suitable (unstable, etc).
Corrections welcome!
Best known
- thttpd
- lighttpd
- fnord
- Boa
- tux
Lesser known
- cherokee
- erlang based ones: yaws, httpd (inets), pico, ...
- thy
- monkey
- AppWeb
- Anti-Web httpd
- abyss
- mini-httpd
- muhttpd
- shttpd
- shttpdj
- Hiawatha
- phttpd
- AHD
- Embedded HTTP Server (EHS) - C++ class
- Seminole (*really* embeddable)
- And-httpd, webfs, darkhttpd, micro_httpd (static only)
- w3server
This is ignoring beta, Java/PHP/Python/Perl/etc implementations.
>
> Hubble
| |
|
|
|
|
|
|
|