IBM nweb POST form functionality - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

IBM nweb POST form functionality

qhimqqhimq Posts: 16Member
Hi

I just started learning about web server/network programming.

I started off here:
http://www.ibm.com/developerworks/systems/library/es-nweb.html

and got the code working after playing around.

Now I'm curious how to implement a post operation from a typical form. I'd rather implement it myself without using php.

Any ideas? My webserver source is:
[code]
/*
** server.c -- a stream socket server demo
http://www.ibm.com/developerworks/systems/library/es-nweb.html
*/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MYPORT 3489 // the port users will be connecting to

#define BUFSIZE 8096
#define ERROR 42
#define SORRY 43
#define LOG 44

#define BACKLOG 10 // how many pending connections queue will hold

struct {
char *ext;
char *filetype;
} extensions [] = {
{"gif", "image/gif" },
{"jpg", "image/jpeg"},
{"jpeg","image/jpeg"},
{"png", "image/png" },
{"htm", "text/html" },
{"html","text/html" },
{0,0} };

void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}

void PRINTlog(int type, char *s1, char *s2, int num)
{
int fd ;
char PRINTlogbuffer[BUFSIZE*2];

switch (type) {
case ERROR:
sprintf(PRINTlogbuffer,
"ERROR: %s:%s Errno=%d exiting pid=%d",
s1, s2, errno,getpid());
break;
case SORRY:
sprintf(PRINTlogbuffer,
"

nweb Web Server Sorry: %s %s

",
s1, s2);
write(num,PRINTlogbuffer,strlen(PRINTlogbuffer));
sprintf(PRINTlogbuffer,"SORRY: %s:%s",s1, s2);
break;
case LOG:
sprintf(PRINTlogbuffer," INFO: %s:%s:%d",
s1, s2,num);
break;
}
//printf("%s",PRINTlogbuffer);
}

void web(int fd, int hit)
{
int j, file_fd, buflen, len;
long i, ret;
char * fstr;
static char buffer[BUFSIZE+1]; /* static so zero filled */

ret =read(fd,buffer,BUFSIZE); /* read Web request in one go */
if(ret == 0 || ret == -1) { /* read failure stop now */
PRINTlog(SORRY,"failed to read browser request","",fd);
}
if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
buffer[ret]=0; /* terminate the buffer */
else buffer[0]=0;

for(i=0;i 0 ) {
(void)write(fd,buffer,ret);
}
#ifdef LINUX
sleep(1); /* to allow socket to drain */
#endif
exit(1);
}


int main(int argc, char **argv)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}

if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("~YourIP~"); // fill with IP
memset(my_addr.sin_zero, '', sizeof my_addr.sin_zero);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}

chdir("/home/user/Desktop/webserver/");
while(1) { // main accept() loop
sin_size = sizeof their_addr;
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s
",
inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process
close(sockfd); // child doesn't need the listener
web(new_fd,0); /* never returns */
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn't need this
}

return 0;
}
[/code]
Sign In or Register to comment.