2009-01-25 09:58:40 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-01-25 09:58:40 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
2010-10-31 07:23:53 +00:00
|
|
|
#ifndef D_HTTP_SERVER_H
|
|
|
|
#define D_HTTP_SERVER_H
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <string>
|
2009-05-08 07:58:50 +00:00
|
|
|
#include <sstream>
|
2010-01-22 14:09:39 +00:00
|
|
|
#include <vector>
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
#include "SharedHandle.h"
|
|
|
|
#include "SocketBuffer.h"
|
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
|
|
|
class SocketCore;
|
|
|
|
class HttpHeader;
|
|
|
|
class HttpHeaderProcessor;
|
|
|
|
class DownloadEngine;
|
2011-01-16 08:27:01 +00:00
|
|
|
class SocketRecvBuffer;
|
2009-01-25 09:58:40 +00:00
|
|
|
|
|
|
|
class HttpServer {
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
SharedHandle<SocketCore> socket_;
|
2011-01-16 08:27:01 +00:00
|
|
|
SharedHandle<SocketRecvBuffer> socketRecvBuffer_;
|
2010-06-21 13:51:56 +00:00
|
|
|
SocketBuffer socketBuffer_;
|
|
|
|
DownloadEngine* e_;
|
|
|
|
SharedHandle<HttpHeaderProcessor> headerProcessor_;
|
|
|
|
SharedHandle<HttpHeader> lastRequestHeader_;
|
|
|
|
uint64_t lastContentLength_;
|
|
|
|
std::stringstream lastBody_;
|
|
|
|
bool keepAlive_;
|
|
|
|
bool gzip_;
|
|
|
|
std::string username_;
|
|
|
|
std::string password_;
|
|
|
|
bool acceptsPersistentConnection_;
|
|
|
|
bool acceptsGZip_;
|
2011-08-18 12:22:48 +00:00
|
|
|
std::string allowOrigin_;
|
2009-01-25 09:58:40 +00:00
|
|
|
public:
|
|
|
|
HttpServer(const SharedHandle<SocketCore>& socket, DownloadEngine* e);
|
|
|
|
|
|
|
|
~HttpServer();
|
|
|
|
|
|
|
|
SharedHandle<HttpHeader> receiveRequest();
|
|
|
|
|
2009-05-08 07:58:50 +00:00
|
|
|
bool receiveBody();
|
|
|
|
|
|
|
|
std::string getBody() const;
|
|
|
|
|
2011-03-13 15:57:05 +00:00
|
|
|
const std::string& getMethod() const;
|
|
|
|
|
2009-05-08 07:58:50 +00:00
|
|
|
const std::string& getRequestPath() const;
|
|
|
|
|
2011-11-05 08:28:48 +00:00
|
|
|
void feedResponse(std::string& text, const std::string& contentType);
|
2009-01-25 09:58:40 +00:00
|
|
|
|
2009-05-09 15:38:23 +00:00
|
|
|
void feedResponse(const std::string& status,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& headers,
|
2011-11-05 08:28:48 +00:00
|
|
|
std::string& text,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& contentType);
|
2009-05-09 15:38:23 +00:00
|
|
|
|
|
|
|
bool authenticate();
|
|
|
|
|
|
|
|
void setUsernamePassword
|
2010-11-14 07:17:55 +00:00
|
|
|
(const std::string& username, const std::string& password);
|
2009-05-09 15:38:23 +00:00
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
ssize_t sendResponse();
|
|
|
|
|
|
|
|
bool sendBufferIsEmpty() const;
|
2009-01-25 10:55:27 +00:00
|
|
|
|
2010-01-22 14:09:39 +00:00
|
|
|
bool supportsPersistentConnection() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return keepAlive_ && acceptsPersistentConnection_;
|
2010-01-22 14:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool supportsGZip() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return gzip_ && acceptsGZip_;
|
2010-01-22 14:09:39 +00:00
|
|
|
}
|
2009-05-08 07:58:50 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
void enableKeepAlive() { keepAlive_ = true; }
|
2009-05-08 07:58:50 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
void disableKeepAlive() { keepAlive_ = false; }
|
2009-05-09 15:38:23 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
void enableGZip() { gzip_ = true; }
|
2010-01-22 14:09:39 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
void disableGZip() { gzip_ = false; }
|
2010-01-22 14:09:39 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
uint64_t getContentLength() const { return lastContentLength_; }
|
2011-01-16 08:52:18 +00:00
|
|
|
|
|
|
|
const SharedHandle<SocketRecvBuffer>& getSocketRecvBuffer() const
|
|
|
|
{
|
|
|
|
return socketRecvBuffer_;
|
|
|
|
}
|
2011-08-18 12:22:48 +00:00
|
|
|
|
|
|
|
void setAllowOrigin(const std::string& allowOrigin)
|
|
|
|
{
|
|
|
|
allowOrigin_ = allowOrigin;
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace aria2
|
|
|
|
|
2010-10-31 07:23:53 +00:00
|
|
|
#endif // D_HTTP_SERVER_H
|