提交 c512ef1067b7eb93c94198a6b5f1c07b39fc2a6a

作者 LJH 李佳桓
1 个父辈 9d27c631

add

正在显示 1 个修改的文件 包含 100 行增加0 行删除
  1 +/*
  2 + * Copyright 2008 Stephen Liu
  3 + * For license terms, see the file COPYING along with this library.
  4 + */
  5 +
  6 +#include <winsock2.h>
  7 +#include <windows.h>
  8 +#include <stdio.h>
  9 +#include <assert.h>
  10 +
  11 +#define _CRTDBG_MAP_ALLOC
  12 +#include <stdlib.h>
  13 +#include <crtdbg.h>
  14 +
  15 +#include "spporting.hpp"
  16 +
  17 +#include "spwin32iocp.hpp"
  18 +#include "spiocpserver.hpp"
  19 +
  20 +#include "sphttp.hpp"
  21 +#include "sphttpmsg.hpp"
  22 +#include "spserver.hpp"
  23 +
  24 +#pragma comment(lib,"ws2_32")
  25 +#pragma comment(lib,"mswsock")
  26 +
  27 +class SP_HttpEchoHandler : public SP_HttpHandler {
  28 +public:
  29 + SP_HttpEchoHandler(){}
  30 + virtual ~SP_HttpEchoHandler(){}
  31 +
  32 + virtual void handle( SP_HttpRequest * request, SP_HttpResponse * response ) {
  33 + response->setStatusCode( 200 );
  34 + response->appendContent( "<html><head>"
  35 + "<title>Welcome to simple http</title>"
  36 + "</head><body>" );
  37 +
  38 + char buffer[ 512 ] = { 0 };
  39 + snprintf( buffer, sizeof( buffer ),
  40 + "<p>The requested URI is : %s.</p>", request->getURI() );
  41 + response->appendContent( buffer );
  42 +
  43 + snprintf( buffer, sizeof( buffer ),
  44 + "<p>Client IP is : %s.</p>", request->getClientIP() );
  45 + response->appendContent( buffer );
  46 +
  47 + int i = 0;
  48 +
  49 + for( i = 0; i < request->getParamCount(); i++ ) {
  50 + snprintf( buffer, sizeof( buffer ),
  51 + "<p>Param - %s = %s<p>", request->getParamName( i ), request->getParamValue( i ) );
  52 + response->appendContent( buffer );
  53 + }
  54 +
  55 + for( i = 0; i < request->getHeaderCount(); i++ ) {
  56 + snprintf( buffer, sizeof( buffer ),
  57 + "<p>Header - %s: %s<p>", request->getHeaderName( i ), request->getHeaderValue( i ) );
  58 + response->appendContent( buffer );
  59 + }
  60 +
  61 + if( NULL != request->getContent() ) {
  62 + response->appendContent( "<p>" );
  63 + response->appendContent( request->getContent(), request->getContentLength() );
  64 + response->appendContent( "</p>" );
  65 + }
  66 +
  67 + response->appendContent( "</body></html>\n" );
  68 + }
  69 +};
  70 +
  71 +class SP_HttpEchoHandlerFactory : public SP_HttpHandlerFactory {
  72 +public:
  73 + SP_HttpEchoHandlerFactory(){}
  74 + virtual ~SP_HttpEchoHandlerFactory(){}
  75 +
  76 + virtual SP_HttpHandler * create() const {
  77 + return new SP_HttpEchoHandler();
  78 + }
  79 +};
  80 +
  81 +int main( int argc, char * argv[] )
  82 +{
  83 + int port = 8080, maxThreads = 10;
  84 + const char * serverType = "hahs";
  85 +
  86 + if( 0 != sp_initsock() ) assert( 0 );
  87 +
  88 + SP_IocpServer server( "", port, new SP_HttpHandlerAdapterFactory( new SP_HttpEchoHandlerFactory() ) );
  89 +
  90 + server.setTimeout( 60 );
  91 + server.setMaxThreads( maxThreads );
  92 + server.setReqQueueSize( 100, "HTTP/1.1 500 Sorry, server is busy now!\r\n" );
  93 +
  94 + server.runForever();
  95 +
  96 + sp_closelog();
  97 +
  98 + return 0;
  99 +}
  100 +
... ...
注册登录 后发表评论