UIP Web Services

Source: Internet
Author: User
Tags sin web services
To: The http://blog.csdn.net/zouw96/article/details/8444438 program uses the webserver demo program written by the author, adding the corresponding comments. Describe the process of implementing the entire Web server. The callback function for the UIP Web service is/WEBSERVER/HTTPD.C in Httpd_appcall (void). The Handle_connection function is then entered.
static void
handle_connection (struct httpd_state *s)
{
  handle_input (s);
  if (s->state = = state_output) {
    handle_output (s);
  }
}
Handle_input (s) actually refers to Pt_thread (Handle_input (struct httpd_state *s)), and others are similar. This is a lightweight, simulated threading environment protothreads. See for details. Handle_input (s) is the parsing of content that the browser requests to the server. If you enter an IP address, it will output http_index_html. If you request a different page, the character (s) will be used as the file name (waiting for Handle_output). From this function you can see that UIP only supports the ' get ' method, and the Get format is:/(file name). (first variable name) = (filled value) & (second variable name) ... such as: 192.168.0.2/file.html?num1=2&num2=2. You can then follow this format to get what data the browser sends to the server. For HTML language related knowledge, please see. PS. If you want to use the ' post ' method, you need to rewrite the function in the format.
Static Pt_thread (handle_input (struct httpd_state *s)) {psock_begin (&s->sin);   Psock_readto (&s->sin, iso_space);
  Wait for the whitespace if (strncmp (S->inputbuf, Http_get, 4)! = 0) {//not get on Exit end Psock_close_exit (&s->sin); } psock_readto (&s->sin, iso_space);
  Wait for whitespace if (s->inputbuf[0]! = Iso_slash) {//not '/' to exit the end of Psock_close_exit (&s->sin); } if (s->inputbuf[1] = = Iso_space) {//If it is a space character, use index_html as the output page strncpy (s->filename, http_index_html, sizeof (S-
  >filename));  } else {S->inputbuf[psock_datalen (&s->sin)-1] = 0;
  Otherwise, the following characters will be used as output page strncpy (S->filename, &s->inputbuf[0], sizeof (s->filename));  }//can process received data///Httpd_log_file (UIP_CONN->RIPADDR, s->filename); */s->state = State_output;

    Set the status bit to output while (1) {psock_readto (&s->sin, ISO_NL);
 if (strncmp (S->inputbuf, http_referer, 8) = = 0) {S->inputbuf[psock_datalen (&s->sin)-2] = 0;     /* Httpd_log (&s->inputbuf[9]); */}} psock_end (&s->sin);
 }
S->state = State_output is followed by the Handle_output (s) function, which is used as the output file. If the previously requested page name is not found in the saved Web page, it will output 404 (the Really famous). Then you will determine whether the request is a Dynamic Web page (shtml is a suffix, of course, you can also define CGI, ASP, and so on), yes, then turn to Handle_script (s). Not just output the page directly. ps. Web pages are written first, and then converted into binary code by character, put into the HTTPD_FSDATA.C, note that the file is the bottom of the structure is used to traverse the entire Web page file, the addition of files need to change here. There is a static Web page has an HTTP header to add, the original demo has.
Static Pt_thread (handle_output (struct httpd_state *s)) {char *ptr;

  Pt_begin (&S->OUTPUTPT);
    if (!httpd_fs_open (S->filename, &s->file)) {//does not find the corresponding page Httpd_fs_open (http_404_html, &s->file);
    strcpy (S->filename, http_404_html);
    Pt_wait_thread (&S->OUTPUTPT, Send_headers (S, http_header_404));
  Pt_wait_thread (&S->OUTPUTPT,//wait for thread completion (404 error header and content) to print 404 page send_file (s));    } else {pt_wait_thread (&S->OUTPUTPT, Send_headers (S, http_header_200));
    Wait for output 200 success ptr = STRCHR (S->filename, iso_period);
      if (ptr! = NULL && strncmp (PTR, http_shtml, 6) = = 0) {//judged to be shtml pt_init (&S->SCRIPTPT);    Pt_wait_thread (&S->OUTPUTPT, Handle_script (s));
    Wait for CGI to process the dynamic page} else {pt_wait_thread (&S->OUTPUTPT,///Not output page Send_file (s));
  }} psock_close (&s->sout); Pt_end (&s->outPUTPT); }
Next is the processing of the Dynamic Web page, Handle_script (s) is handled this way: traverse the page character, hit%. : (space) The file that corresponds to the output of the following character. Hit%. (space) executes the function corresponding to the following character (plus an external parameter) such as:%. Analog num1. After these parameters are finished, add a carriage return character. Dynamic Web pages and static Web pages in the design of the difference is: suffix and%. : and%. identifier.
Static Pt_thread (handle_script (struct httpd_state *s)) {char *ptr;


  Pt_begin (&S->SCRIPTPT); while (S->file.len > 0) {/* Check if we should start executing a script. */if (*s->file.data = = Iso_perc
      ENT && * (s->file.data + 1) = = Iso_bang) {s->scriptptr = S->file.data + 3;
      S->scriptlen = s->file.len-3; 
     if (* (s->scriptptr-1) = = Iso_colon) {//If it is%!: Open the subsequent corresponding file and output Httpd_fs_open (s->scriptptr + 1, &s->file);
      Pt_wait_thread (&S->SCRIPTPT, Send_file (s)); } else {pt_wait_thread (&s->scriptpt, httpd_cgi (s->scriptptr) (S, s->scriptptr));//If it is% ! Go back to the corresponding program}//S->scriptptr for%!: Next character
      
      _scriptstate (s); /* The script is over, so we reset the pointers and continue sending the rest of the file.
      */S->file.data = s->scriptptr; S->File.len = s->scriptlen; } else {/* see if we find the ' start of script marker in the block ' of HTML to be sent. */if (s->file.
      Len > Uip_mss ()) {S->len = Uip_mss ();
      } else {S->len = s->file.len;
      } if (*s->file.data = = iso_percent) {ptr = STRCHR (s->file.data + 1, iso_percent);
      } else {ptr = STRCHR (S->file.data, iso_percent);
     } if (ptr! = NULL && ptr! = s->file.data) {S->len = (int) (ptr-s->file.data);
     if (S->len >= Uip_mss ()) {S->len = Uip_mss ();   }} pt_wait_thread (&S->SCRIPTPT, Send_part_of_file (s));
      Pages waiting to print without variables section s->file.data + = s->len;
      
    S->file.len-= s->len;
}} pt_end (&S->SCRIPTPT); }
Basically almost, and then exactly what to do with the results of the dynamic output to the Web page, these in the Http-cgi.c file. This function must be defined first, as follows:
Httpd_cgi_call (file, "File-stats", file_stats);               Pointer, the name of the Web data, the name of the callback function
Httpd_cgi_call (TCP, "Tcp-connections", tcp_stats);
Httpd_cgi_call (NET, "Net-stats", net_stats);
static const struct Httpd_cgi_call *calls[] = {&file, &tcp, &net, NULL};
The lookup function name is then traversed to see if it is consistent with the request. Then execute the psock_generator_send (&s->sout, #,*) #是指向下个函数的指针. * is the parameter to the next function, if you design the page to reserve an external parameter can be *=STRCHR (PTR, ") + 1 (the space behind, so in the design of the time to pay attention to). After processing, we changed the output of the dynamic result. snprintf (char *) Uip_appdata, uip_appdata_size, "%8.3f", Analog_count (f)), using this function, where analo_count is my custom function. Ok. Basically simple Web Dynamic Web page has no problem, and some do not understand the site can see the project notes. Click Download. If you use HG as long as clone https://zouw96@bitbucket.org/zouw96/uip_http. For questions about porting, see the previous article.
static unsigned short generate_file_stats (void *arg) {char *f = (char *) arg;
ARG for the following external command parameter return snprintf ((char *) Uip_appdata, uip_appdata_size, "%8.3f", Analog_count (f)); }/*---------------------------------------------------------------------------*/static Pt_thread (File_stats (

  struct Httpd_state *s, char *ptr)) {psock_begin (&s->sout);  Psock_generator_send (&s->sout, Generate_file_stats, STRCHR (PTR, ') + 1);
The pointer points to the outer command argument after the space, Arg psock_end (&s->sout); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.