⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handleform.c

📁 this gives details of the network programming
💻 C
字号:
/* Simple CGI program that returns grades *//* This program assumes each request will include the   following fields (coming from an HTML form):   fname : first name   lname : last name   ssn   : Social Security Number   The program looks up the grades for the individual   corresponding to the fields and returns a list    of grades. We don't actually have a database of    grades (or names, or anything else) - the grades   are just created randomly...*/#include <stdio.h>#include <string.h>#include <sys/types.h>#include <time.h>#include <stdlib.h>#include "cgilib.h"	/* simple CGI request parsing routines */#define MAXFIELDS 10 	/* Max number of fields we will handle *//* print HTML error message and exit */void fatal_error( char *s) {  printf("<H1 ALIGN=CENTER><FONT COLOR=RED>\n");  printf("%s</FONT></H1>\n",s);  exit(1);}/* see if a string contains any nonblank chars */int empty( char *s) {  return(strlen(s)==strspn( s, " \t\n"));}char *grades[] = {  "Test#1", "Test#2", "Test#2", "HW#1", "HW#2", "HW#3"};int n_grades = sizeof(grades)/sizeof(char *);int random_grade(void) {  static int seeded=0;  if (!seeded) {    srand(time(NULL));    seeded=1;  }  return(50 + (int) (50.0*rand()/(RAND_MAX+1.0)));}/* generate_grade_table just makes up the grades, there   is no database!!!!!*/void generate_grade_table(char *f, char *l, char *s) {  int i,tot,grade;  /* print the student name and SSN */  printf("<CENTER><H2>%s %s (%s) </CENTER>\n",f,l,s);  /* print the grade table */  printf("<CENTER>\n");  printf("<TABLE BORDER=3>\n");  printf("<TR>\n");  for (i=0;i<n_grades;i++)     printf(" <TH>%s</TH>\n",grades[i]);  printf("<TH>Final<BR>Average</TH>\n");  printf("</TR>\n");  printf("<TR>\n");  tot=0;  for (i=0;i<n_grades;i++) {    grade = random_grade();    tot += grade;    printf(" <TD>%d</TD>\n",grade);  }  printf(" <TD>%.2f</TD>\n",tot/(double)n_grades);  printf("</TR>\n");  printf("</TABLE>\n\n");  printf("</CENTER>\n");}int main() {  char *query;  char *names[MAXFIELDS];  char *vals[MAXFIELDS];	  char *lname,*fname,*ssn;  int i,n;  /* always tell the browser what kind of document we are sending */  /* NOTE: the web server that started the CGI program will send     back the appropriate HTTP header !!! */  printf("Content-type: text/html\n\n");  /* now send some HTML stuff */  printf("<BODY BGCOLOR=#FFFFFF>\n");  printf("<H2 ALIGN=CENTER>Netprog grades delivered to your desktop</H2>\n");  query = get_request();  if (query==NULL) {    /* we got nothing, or there is an error */    /* send back an HTML error message */    fatal_error("ERROR - Invalid query or something");  }  /* parse the request string and chop into fields */  n = split_and_parse(query,names,vals,MAXFIELDS);  /* If we got more then 3 fields, this is a bogus request */  if (n!=3)     fatal_error("ERROR - Invalid query or something");  /* this is a crummy way to do it, we really need an associative     array for handling the fields (C++?).     */  lname=fname=ssn=NULL;  for (i=0;i<3;i++) {    if (strcasecmp(names[i],"fname")==0) {      fname = vals[i];    } else if (strcasecmp(names[i],"lname")==0) {      lname = vals[i];    } else if (strcasecmp(names[i],"ssn")==0) {      ssn = vals[i];    } else {      /* unknown field name - this is an error */      fatal_error("ERROR - Invalid field or something");      exit(1);    }  }  /* Make sure we got them all */  if ((!lname) || (!fname) || (!ssn)) {      /* missing field name - this is an error */      printf("ERROR - Missing field");      exit(1);  }  /* Make sure something was entered for each field */  if (empty(fname))     fatal_error("Error - no first name entered");  else if (empty(lname))    fatal_error("Error - no last name entered");  else if (empty(ssn))    fatal_error("Error - no SSN entered");  /* OK - everything checks out - now process the request */  generate_grade_table(fname,lname,ssn);  return(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -