Skip to content

Commit

Permalink
fastcgi sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed Apr 1, 2015
1 parent 1661eb1 commit b3f239b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/fastcgi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_executable(fastcgi_test fastcgi.cc fastcgi_test.cc)
add_executable(fastcgi_test fastcgi.cc fastcgi_test.cc ../sudoku/sudoku.cc)
target_link_libraries(fastcgi_test muduo_net)

36 changes: 30 additions & 6 deletions examples/fastcgi/fastcgi_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <examples/fastcgi/fastcgi.h>
#include <examples/sudoku/sudoku.h>

#include <muduo/base/Logging.h>
#include <muduo/net/EventLoop.h>
Expand All @@ -8,41 +9,64 @@

using namespace muduo::net;

const string kPath = "/sudoku/";

void onRequest(const TcpConnectionPtr& conn,
FastCgiCodec::ParamMap& params,
Buffer* in)
{
LOG_INFO << conn->name() << ": " << params["REQUEST_URI"];
string uri = params["REQUEST_URI"];
LOG_INFO << conn->name() << ": " << uri;

for (FastCgiCodec::ParamMap::const_iterator it = params.begin();
it != params.end(); ++it)
it != params.end(); ++it)
{
LOG_DEBUG << it->first << " = " << it->second;
}
Buffer response;
response.append("Context-Type: text/plain\r\n\r\n");
response.append("Hello FastCGI.");
if (uri.size() == kCells + kPath.size() && uri.find(kPath) == 0)
{
response.append(solveSudoku(uri.substr(kPath.size())));
}
else
{
// FIXME: set http status code 400
response.append("bad request");
}

FastCgiCodec::respond(&response);
conn->send(&response);
}

typedef boost::shared_ptr<FastCgiCodec> CodecPtr;
void onConnection(const TcpConnectionPtr& conn)
{
if (conn->connected())
{
typedef boost::shared_ptr<FastCgiCodec> CodecPtr;
CodecPtr codec(new FastCgiCodec(onRequest));
conn->setContext(codec);
conn->setMessageCallback(
boost::bind(&FastCgiCodec::onMessage, codec, _1, _2, _3));
conn->setTcpNoDelay(true);
}
}

int main()
int main(int argc, char* argv[])
{
int port = 19981;
int threads = 0;
if (argc > 1)
port = atoi(argv[1]);
if (argc > 2)
threads = atoi(argv[2]);
InetAddress addr(static_cast<uint16_t>(port));
LOG_INFO << "Sudoku FastCGI listens on " << addr.toIpPort()
<< " threads " << threads;
muduo::net::EventLoop loop;
TcpServer server(&loop, InetAddress(9000), "FastCGI");
TcpServer server(&loop, addr, "FastCGI");
server.setConnectionCallback(onConnection);
server.setThreadNum(threads);
server.start();
loop.loop();
}
31 changes: 26 additions & 5 deletions examples/fastcgi/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ http {
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;
access_log off;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

upstream muduo_backend {
server localhost:10000;
server localhost:19981;
#server localhost:19982;
keepalive 32;
}

Expand All @@ -55,12 +56,32 @@ http {
root html;
}

# pass /fcgi/ to muduo FastCGI server listening on 127.0.0.1:10000
# pass /sudoku/ to muduo FastCGI server listening on 127.0.0.1:19981
#
location /fcgi/ {
location /sudoku/ {
fastcgi_keep_conn on;
fastcgi_pass muduo_backend;
include fastcgi_params;
#include fastcgi_params;
#fastcgi_param QUERY_STRING $query_string;
#fastcgi_param REQUEST_METHOD $request_method;
#fastcgi_param CONTENT_TYPE $content_type;
#fastcgi_param CONTENT_LENGTH $content_length;

#fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
#fastcgi_param DOCUMENT_URI $document_uri;
#fastcgi_param DOCUMENT_ROOT $document_root;
#fastcgi_param SERVER_PROTOCOL $server_protocol;
#fastcgi_param HTTPS $https if_not_empty;

#fastcgi_param GATEWAY_INTERFACE CGI/1.1;
#fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

#fastcgi_param REMOTE_ADDR $remote_addr;
#fastcgi_param REMOTE_PORT $remote_port;
#fastcgi_param SERVER_ADDR $server_addr;
#fastcgi_param SERVER_PORT $server_port;
#fastcgi_param SERVER_NAME $server_name;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
Expand Down

0 comments on commit b3f239b

Please sign in to comment.