提交 9c819d9d69a37745ddd07dbfe93e4882bd2cd427

作者 LJH 李佳桓
1 个父辈 3a708a05

add

正在显示 1 个修改的文件 包含 132 行增加0 行删除
  1 +/*
  2 + * Copyright 2008 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 "spdispatcher.hpp"
  18 +#include "spioutils.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 +
  78 + extern char *optarg ;
  79 + int c ;
  80 +
  81 + while( ( c = getopt ( argc, argv, "p:t:v" )) != EOF ) {
  82 + switch ( c ) {
  83 + case 'p' :
  84 + port = atoi( optarg );
  85 + break;
  86 + case 't':
  87 + maxThreads = atoi( optarg );
  88 + break;
  89 + case '?' :
  90 + case 'v' :
  91 + printf( "Usage: %s [-p <port>] [-t <threads>]\n", argv[0] );
  92 + exit( 0 );
  93 + }
  94 + }
  95 +
  96 + //openlog( "testhttp_d", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER );
  97 +
  98 + int maxConnections = 10000, reqQueueSize = 10000;
  99 + const char * refusedMsg = "HTTP/1.1 500 Sorry, server is busy now!\r\n";
  100 +
  101 + SP_HttpHandlerAdapterFactory factory( new SP_HttpEchoHandlerFactory() );
  102 +
  103 + int listenFd = -1;
  104 + if( 0 == SP_IOUtils::tcpListen( "", port, &listenFd ) ) {
  105 + SP_Dispatcher dispatcher( new SP_DefaultCompletionHandler(), maxThreads );
  106 + dispatcher.dispatch();
  107 + dispatcher.setTimeout( 60 );
  108 +
  109 + for( ; ; ) {
  110 + struct sockaddr_in addr;
  111 + socklen_t socklen = sizeof( addr );
  112 + int fd = accept( listenFd, (struct sockaddr*)&addr, &socklen );
  113 +
  114 + if( fd > 0 ) {
  115 + if( dispatcher.getSessionCount() >= maxConnections
  116 + || dispatcher.getReqQueueLength() >= reqQueueSize ) {
  117 + write( fd, refusedMsg, strlen( refusedMsg ) );
  118 + close( fd );
  119 + } else {
  120 + dispatcher.push( fd, factory.create() );
  121 + }
  122 + } else {
  123 + break;
  124 + }
  125 + }
  126 + }
  127 +
  128 + closelog();
  129 +
  130 + return 0;
  131 +}
  132 +
... ...
注册登录 后发表评论