2008-11-30 02:01:58 +01:00
|
|
|
// miniwebserver.cpp
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-11-30 02:01:58 +01:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-11-30 02:01:58 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-11-30 02:01:58 +01:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "miniwebserver.h"
|
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
MiniWebServer::MiniWebServer() {
|
2008-11-30 02:01:58 +01:00
|
|
|
sock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MiniWebServer::init(int port) {
|
|
|
|
SockAddr me(port);
|
2008-12-29 02:28:49 +01:00
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if ( sock == INVALID_SOCKET ) {
|
|
|
|
log() << "ERROR: MiniWebServer listen(): invalid socket? " << errno << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
prebindOptions( sock );
|
|
|
|
if ( ::bind(sock, (sockaddr *) &me.sa, me.addressSize) != 0 ) {
|
2008-11-30 02:01:58 +01:00
|
|
|
log() << "MiniWebServer: bind() failed port:" << port << " errno:" << errno << endl;
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( errno == 98 )
|
|
|
|
log() << "98 == addr already in use" << endl;
|
|
|
|
closesocket(sock);
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-30 02:01:58 +01:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( ::listen(sock, 16) != 0 ) {
|
|
|
|
log() << "MiniWebServer: listen() failed " << errno << endl;
|
|
|
|
closesocket(sock);
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-30 02:01:58 +01:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
return true;
|
2008-11-30 02:01:58 +01:00
|
|
|
}
|
|
|
|
|
2008-12-31 20:58:42 +01:00
|
|
|
string MiniWebServer::parseURL( const char * buf ){
|
2008-12-31 03:58:36 +01:00
|
|
|
char * urlStart = strstr( buf , " " );
|
|
|
|
if ( ! urlStart )
|
|
|
|
return "/";
|
|
|
|
|
|
|
|
urlStart++;
|
|
|
|
|
|
|
|
char * end = strstr( urlStart , " " );
|
|
|
|
if ( ! end ){
|
|
|
|
end = strstr( urlStart , "\r" );
|
|
|
|
if ( ! end ){
|
|
|
|
end = strstr( urlStart , "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! end )
|
|
|
|
return "/";
|
|
|
|
|
|
|
|
int diff = (int)(end-urlStart);
|
|
|
|
if ( diff < 0 || diff > 255 )
|
|
|
|
return "/";
|
|
|
|
|
|
|
|
return string( urlStart , (int)(end-urlStart) );
|
|
|
|
}
|
|
|
|
|
2008-12-31 20:58:42 +01:00
|
|
|
void MiniWebServer::parseParams( map<string,string> & params , string query ){
|
|
|
|
if ( query.size() == 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ( query.size() ){
|
|
|
|
|
|
|
|
string::size_type amp = query.find( "&" );
|
|
|
|
|
|
|
|
string cur;
|
|
|
|
if ( amp == string::npos ){
|
|
|
|
cur = query;
|
|
|
|
query = "";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cur = query.substr( 0 , amp );
|
|
|
|
query = query.substr( amp + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
string::size_type eq = cur.find( "=" );
|
|
|
|
if ( eq == string::npos )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
params[cur.substr(0,eq)] = cur.substr(eq+1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string MiniWebServer::parseMethod( const char * headers ){
|
|
|
|
char * end = strstr( headers , " " );
|
|
|
|
if ( ! end )
|
|
|
|
return "GET";
|
|
|
|
return string( headers , (int)(end-headers) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 02:01:58 +01:00
|
|
|
void MiniWebServer::accepted(int s) {
|
|
|
|
char buf[4096];
|
|
|
|
int x = ::recv(s, buf, sizeof(buf)-1, 0);
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( x <= 0 )
|
2008-11-30 02:01:58 +01:00
|
|
|
return;
|
|
|
|
buf[x] = 0;
|
|
|
|
|
|
|
|
string responseMsg;
|
|
|
|
int responseCode = 599;
|
|
|
|
vector<string> headers;
|
2008-12-31 03:58:36 +01:00
|
|
|
doRequest(buf, parseURL( buf ), responseMsg, responseCode, headers);
|
2008-11-30 02:01:58 +01:00
|
|
|
|
|
|
|
stringstream ss;
|
|
|
|
ss << "HTTP/1.0 " << responseCode;
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( responseCode == 200 ) ss << " OK";
|
2008-11-30 02:01:58 +01:00
|
|
|
ss << "\r\n";
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( headers.empty() ) {
|
2008-11-30 02:01:58 +01:00
|
|
|
ss << "Content-Type: text/html\r\n";
|
|
|
|
}
|
|
|
|
else {
|
2008-12-29 02:28:49 +01:00
|
|
|
for ( vector<string>::iterator i = headers.begin(); i != headers.end(); i++ )
|
2008-11-30 02:01:58 +01:00
|
|
|
ss << *i << "\r\n";
|
|
|
|
}
|
|
|
|
ss << "\r\n";
|
|
|
|
ss << responseMsg;
|
|
|
|
string response = ss.str();
|
|
|
|
|
|
|
|
::send(s, response.c_str(), response.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MiniWebServer::run() {
|
2008-12-29 02:28:49 +01:00
|
|
|
SockAddr from;
|
|
|
|
while ( 1 ) {
|
|
|
|
int s = accept(sock, (sockaddr *) &from.sa, &from.addressSize);
|
|
|
|
if ( s < 0 ) {
|
|
|
|
log() << "MiniWebServer: accept() returns " << s << " errno:" << errno << endl;
|
2008-11-30 02:01:58 +01:00
|
|
|
sleepmillis(200);
|
2008-12-29 02:28:49 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
disableNagle(s);
|
2008-11-30 02:01:58 +01:00
|
|
|
RARELY log() << "MiniWebServer: connection accepted from " << from.toString() << endl;
|
2008-12-29 02:28:49 +01:00
|
|
|
accepted( s );
|
2008-11-30 02:01:58 +01:00
|
|
|
closesocket(s);
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2008-11-30 02:01:58 +01:00
|
|
|
}
|