提交 3a708a059cdca9565dc9278cf49abcfe9fa71dae

作者 LJH 李佳桓
1 个父辈 32e7eea6

add

正在显示 1 个修改的文件 包含 128 行增加0 行删除
  1 +/*
  2 + * Copyright 2007 Stephen Liu
  3 + * For license terms, see the file COPYING along with this library.
  4 + */
  5 +
  6 +#include <string.h>
  7 +#include <stdio.h>
  8 +#include <stdlib.h>
  9 +#include <string.h>
  10 +#include <signal.h>
  11 +#include <assert.h>
  12 +
  13 +#include "spporting.hpp"
  14 +
  15 +#include "sphttp.hpp"
  16 +#include "sphttpmsg.hpp"
  17 +#include "spserver.hpp"
  18 +#include "splfserver.hpp"
  19 +
  20 +class SP_HttpEchoHandler : public SP_HttpHandler {
  21 +public:
  22 + SP_HttpEchoHandler(){}
  23 + virtual ~SP_HttpEchoHandler(){}
  24 +
  25 + virtual void handle( SP_HttpRequest * request, SP_HttpResponse * response ) {
  26 + response->setStatusCode( 200 );
  27 + response->appendContent( "<html><head>"
  28 + "<title>Welcome to simple http</title>"
  29 + "</head><body>" );
  30 +
  31 + char buffer[ 512 ] = { 0 };
  32 + snprintf( buffer, sizeof( buffer ),
  33 + "<p>The requested URI is : %s.</p>", request->getURI() );
  34 + response->appendContent( buffer );
  35 +
  36 + snprintf( buffer, sizeof( buffer ),
  37 + "<p>Client IP is : %s.</p>", request->getClientIP() );
  38 + response->appendContent( buffer );
  39 +
  40 + int i = 0;
  41 +
  42 + for( i = 0; i < request->getParamCount(); i++ ) {
  43 + snprintf( buffer, sizeof( buffer ),
  44 + "<p>Param - %s = %s<p>", request->getParamName( i ), request->getParamValue( i ) );
  45 + response->appendContent( buffer );
  46 + }
  47 +
  48 + for( i = 0; i < request->getHeaderCount(); i++ ) {
  49 + snprintf( buffer, sizeof( buffer ),
  50 + "<p>Header - %s: %s<p>", request->getHeaderName( i ), request->getHeaderValue( i ) );
  51 + response->appendContent( buffer );
  52 + }
  53 +
  54 + if( NULL != request->getContent() ) {
  55 + response->appendContent( "<p>" );
  56 + response->appendContent( request->getContent(), request->getContentLength() );
  57 + response->appendContent( "</p>" );
  58 + }
  59 +
  60 + response->appendContent( "</body></html>\n" );
  61 + }
  62 +};
  63 +
  64 +class SP_HttpEchoHandlerFactory : public SP_HttpHandlerFactory {
  65 +public:
  66 + SP_HttpEchoHandlerFactory(){}
  67 + virtual ~SP_HttpEchoHandlerFactory(){}
  68 +
  69 + virtual SP_HttpHandler * create() const {
  70 + return new SP_HttpEchoHandler();
  71 + }
  72 +};
  73 +
  74 +int main( int argc, char * argv[] )
  75 +{
  76 + int port = 8080, maxThreads = 10;
  77 + const char * serverType = "lf";
  78 +
  79 +#ifndef WIN32
  80 + extern char *optarg ;
  81 + int c ;
  82 +
  83 + while( ( c = getopt ( argc, argv, "p:t:s:v" )) != EOF ) {
  84 + switch ( c ) {
  85 + case 'p' :
  86 + port = atoi( optarg );
  87 + break;
  88 + case 't':
  89 + maxThreads = atoi( optarg );
  90 + break;
  91 + case 's':
  92 + serverType = optarg;
  93 + break;
  94 + case '?' :
  95 + case 'v' :
  96 + printf( "Usage: %s [-p <port>] [-t <threads>] [-s <hahs|lf>]\n", argv[0] );
  97 + exit( 0 );
  98 + }
  99 + }
  100 +#endif
  101 +
  102 + sp_openlog( "testhttp", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER );
  103 +
  104 + assert( 0 == sp_initsock() );
  105 +
  106 + if( 0 == strcasecmp( serverType, "hahs" ) ) {
  107 + SP_Server server( "", port, new SP_HttpHandlerAdapterFactory( new SP_HttpEchoHandlerFactory() ) );
  108 +
  109 + server.setTimeout( 60 );
  110 + server.setMaxThreads( maxThreads );
  111 + server.setReqQueueSize( 100, "HTTP/1.1 500 Sorry, server is busy now!\r\n" );
  112 +
  113 + server.runForever();
  114 + } else {
  115 + SP_LFServer server( "", port, new SP_HttpHandlerAdapterFactory( new SP_HttpEchoHandlerFactory() ) );
  116 +
  117 + server.setTimeout( 60 );
  118 + server.setMaxThreads( maxThreads );
  119 + server.setReqQueueSize( 100, "HTTP/1.1 500 Sorry, server is busy now!\r\n" );
  120 +
  121 + server.runForever();
  122 + }
  123 +
  124 + sp_closelog();
  125 +
  126 + return 0;
  127 +}
  128 +
... ...
注册登录 后发表评论