Spwebserver: A spserver-based Web server framework

Source: Internet
Author: User

Spwebserver: A spserver-based Web server framework Blog Category:
    • OpenSource Project
Application Server Framework Web network application multithreading see this topic, it is estimated that many people will ask: Why do you want to implement a Web server?

Here are a few reasons:

1. This is a Web server framework, not a full Web server. This means that Spwebserver provides a set of APIs and class libraries that can be easily integrated into existing applications. You can call Spwebserver a embedded Web server.

2. Sometimes what we need is not a powerful and complete Web server (Apache, for example), we just need a server framework that provides the most basic HTTP functionality. For example, to implement the XML-RPC,JSON-RPC server-side with A/C + +, or to implement an HTTP bind for Jabberd. In these scenarios, Apache can also be used, but using embedded Web server can simplify configuration.

3. In the following situations, Apache seems inappropriate, and embedded Web server is just right: in an existing application, the HTTP interface is used to provide some simple control and statistical functions.

Based on the above reasons, also for
1. Enrich the Spserver project (please refer to: spserver: A high concurrent server framework based on Half-sync/half-async mode);
2. Just as a good example of the spserver framework;
Therefore, a Web server framework is implemented based on the Spserver framework.

Source code Download:
Http://spserver.googlecode.com/files/spserver-0.5.src.tar.gz
Http://code.google.com/p/spserver/downloads/list

Let's look at a simple example of using Spwebserver.

Java code
  1. Class Sp_httpechohandler: Public Sp_httphandler {
  2. Public
  3. Sp_httpechohandler () {}
  4. Virtual ~sp_httpechohandler () {}
  5. Virtual void handle (Sp_httprequest * request, Sp_httpresponse * response) {
  6. Response->setstatuscode ( 200);
  7. Response->appendcontent ( "
  8. "<title>welcome to Simple http</title>"
  9. "
  10. char buffer[ ] = { 0};
  11. snprintf (buffer, sizeof (buffer),
  12. "<p>the requested URI is:%s.</p>", Request->geturi ());
  13. Response->appendcontent (buffer);
  14. snprintf (buffer, sizeof (buffer),
  15. "<p>client IP is:%s.</p>", Request->getclientip ());
  16. Response->appendcontent (buffer);
  17. For ( int i = 0; i < Request->getparamcount (); i++) {
  18. snprintf (buffer, sizeof (buffer),
  19. "<p>param-%s =%s<p>", Request->getparamname (i),
  20. Request->getparamvalue (i));
  21. Response->appendcontent (buffer);
  22. }
  23. For ( int i = 0; i < Request->getheadercount (); i++) {
  24. snprintf (buffer, sizeof (buffer),
  25. "<p>header-%s:%s<p>", Request->getheadername (i),
  26. Request->getheadervalue (i));
  27. Response->appendcontent (buffer);
  28. }
  29. if (NULL! = Request->getcontent ()) {
  30. Response->appendcontent ( "<p>");
  31. Response->appendcontent (Request->getcontent (),
  32. Request->getcontentlength ());
  33. Response->appendcontent ( "</p>");
  34. }
  35. Response->appendcontent ( "</body>
  36. }
  37. };
  38. Class Sp_httpechohandlerfactory: Public sp_httphandlerfactory {
  39. Public
  40. Sp_httpechohandlerfactory () {}
  41. Virtual ~sp_httpechohandlerfactory () {}
  42. Virtual Sp_httphandler * Create () Const {
  43. return new Sp_httpechohandler ();
  44. }
  45. };
  46. //---------------------------------------------------------
  47. int main ( int argc, char * argv[])
  48. {
  49. int port = 8080;
  50. Sp_server Server ( "", Port,
  51. New Sp_httphandleradapterfactory ( new Sp_httpechohandlerfactory ()));
  52. Server.runforever ();
  53. return 0;
  54. }


The above code shows an HTTP echo server, which displays the request information sent by the client on the page.

In the simplest case, using spwebserver requires implementing two classes: Subclasses of Sp_httphandler and subclasses of Sp_httphandlerfactory.
The subclass of Sp_httphandler is responsible for handling specific HTTP requests.
Subclass of Sp_httphandlerfactory assists Spserver to create a Sp_httphandler subclass instance for each connection.

In the implementation process, the Adapter model was used to adapt the Sp_httphandler and sp_httphandlerfactory to Sp_handler and sp_handleradapterfactory. These two Adapter have been implemented and do not need to be implemented by users.

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.