2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package 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.
|
|
|
|
*/
|
|
|
|
|
2002-05-30 16:13:16 +00:00
|
|
|
#ifndef CHTTPPROTOCOL_H
|
|
|
|
#define CHTTPPROTOCOL_H
|
|
|
|
|
|
|
|
#include "CString.h"
|
2003-01-04 22:01:32 +00:00
|
|
|
#include "CStringUtil.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include "BasicTypes.h"
|
2002-06-02 13:34:35 +00:00
|
|
|
#include "stdlist.h"
|
2002-06-01 19:26:11 +00:00
|
|
|
#include "stdmap.h"
|
|
|
|
#include "stdvector.h"
|
2002-05-30 16:13:16 +00:00
|
|
|
|
|
|
|
class IInputStream;
|
|
|
|
class IOutputStream;
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! HTTP request type
|
|
|
|
/*!
|
|
|
|
This class encapsulates an HTTP request.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
class CHTTPRequest {
|
2002-07-28 17:55:59 +00:00
|
|
|
private:
|
2002-06-02 13:34:35 +00:00
|
|
|
typedef std::list<std::pair<CString, CString> > CHeaderList;
|
2002-07-28 17:55:59 +00:00
|
|
|
public:
|
|
|
|
//! Iterator on headers
|
|
|
|
/*!
|
|
|
|
An iterator on the headers. Each element is a std::pair; first is
|
|
|
|
the header name as a CString, second is the header value as a CString.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
typedef CHeaderList::const_iterator const_iterator;
|
2002-05-30 16:13:16 +00:00
|
|
|
|
2002-06-02 13:34:35 +00:00
|
|
|
CHTTPRequest();
|
|
|
|
~CHTTPRequest();
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! @name manipulators
|
|
|
|
//@{
|
2002-06-02 13:34:35 +00:00
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Insert header
|
|
|
|
/*!
|
|
|
|
Add a header by name replacing the existing header, if any.
|
|
|
|
Headers are sent in the order they're inserted. Replacing
|
|
|
|
a header does not change its original position in the order.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
void insertHeader(const CString& name, const CString& value);
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Append header
|
|
|
|
/*!
|
|
|
|
Append a header. Equivalent to insertHeader() if the header
|
|
|
|
doesn't exist, otherwise it appends a comma and the value to
|
|
|
|
the existing header.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
void appendHeader(const CString& name, const CString& value);
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Remove header
|
|
|
|
/*!
|
|
|
|
Remove a header by name. Does nothing if the header doesn't exist.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
void eraseHeader(const CString& name);
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//@}
|
|
|
|
//! @name accessors
|
|
|
|
//@{
|
2002-06-02 13:34:35 +00:00
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Check header existence
|
|
|
|
/*!
|
|
|
|
Returns true iff the header exists.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
bool isHeader(const CString& name) const;
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Get header
|
|
|
|
/*!
|
|
|
|
Get a header by name. Returns the empty string if the header
|
|
|
|
doesn't exist.
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
CString getHeader(const CString& name) const;
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
// headers are iterated in the order they were added.
|
|
|
|
//! Get beginning header iterator
|
2002-06-02 13:34:35 +00:00
|
|
|
const_iterator begin() const { return m_headers.begin(); }
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Get ending header iterator
|
2002-06-02 13:34:35 +00:00
|
|
|
const_iterator end() const { return m_headers.end(); }
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//@}
|
|
|
|
|
2002-06-02 13:34:35 +00:00
|
|
|
public:
|
|
|
|
// note -- these members are public for convenience
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP method
|
2002-05-30 16:13:16 +00:00
|
|
|
CString m_method;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP URI
|
2002-05-30 16:13:16 +00:00
|
|
|
CString m_uri;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP major version number
|
2002-05-30 16:13:16 +00:00
|
|
|
SInt32 m_majorVersion;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP minor version number
|
2002-05-30 16:13:16 +00:00
|
|
|
SInt32 m_minorVersion;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP body, after transfer decoding
|
2002-06-02 13:34:35 +00:00
|
|
|
CString m_body;
|
2002-05-30 16:13:16 +00:00
|
|
|
|
2002-06-02 13:34:35 +00:00
|
|
|
private:
|
2002-07-28 17:55:59 +00:00
|
|
|
typedef std::map<CString, CHeaderList::iterator,
|
|
|
|
CStringUtil::CaselessCmp> CHeaderMap;
|
|
|
|
|
2002-05-30 16:13:16 +00:00
|
|
|
CHeaderList m_headers;
|
2002-06-02 13:34:35 +00:00
|
|
|
CHeaderMap m_headerByName;
|
2002-05-30 16:13:16 +00:00
|
|
|
};
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! HTTP reply type
|
|
|
|
/*!
|
|
|
|
This class encapsulates an HTTP reply.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
class CHTTPReply {
|
|
|
|
public:
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Header list
|
|
|
|
/*!
|
|
|
|
The type of the reply header list. Each pair is the header name
|
|
|
|
and value, respectively for first and second.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
typedef std::vector<std::pair<CString, CString> > CHeaderList;
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
// note -- these members are public for convenience
|
|
|
|
//! The HTTP major version number
|
2002-05-30 16:13:16 +00:00
|
|
|
SInt32 m_majorVersion;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP minor version number
|
2002-05-30 16:13:16 +00:00
|
|
|
SInt32 m_minorVersion;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP status code
|
2002-05-30 16:13:16 +00:00
|
|
|
SInt32 m_status;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP reason phrase
|
2002-05-30 16:13:16 +00:00
|
|
|
CString m_reason;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP method
|
2002-05-30 16:13:16 +00:00
|
|
|
CString m_method;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP headers
|
2002-05-30 16:13:16 +00:00
|
|
|
CHeaderList m_headers;
|
2002-07-28 17:55:59 +00:00
|
|
|
//! The HTTP body
|
2002-05-30 16:13:16 +00:00
|
|
|
CString m_body;
|
|
|
|
};
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! HTTP protocol utilities
|
|
|
|
/*!
|
|
|
|
This class provides utility functions for HTTP.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
class CHTTPProtocol {
|
|
|
|
public:
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Multipart form parts
|
|
|
|
/*!
|
|
|
|
Each element is the contents of a multipart form part indexed by
|
|
|
|
it's name.
|
|
|
|
*/
|
|
|
|
typedef std::map<CString, CString> CFormParts;
|
|
|
|
|
|
|
|
//! Read HTTP request
|
|
|
|
/*!
|
|
|
|
Read and parse an HTTP request. The result is returned in a
|
|
|
|
CHTTPRequest which the client must delete. Throws an
|
|
|
|
XHTTP if there was a parse error. Throws an XIO exception
|
|
|
|
if there was a read error. If \c maxSize is greater than
|
|
|
|
zero and the request is larger than \c maxSize bytes then
|
|
|
|
throws XHTTP(413) (request entity too large).
|
|
|
|
*/
|
2002-06-02 13:34:35 +00:00
|
|
|
static CHTTPRequest* readRequest(IInputStream*, UInt32 maxSize = 0);
|
2002-05-30 16:13:16 +00:00
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Send HTTP response
|
|
|
|
/*!
|
|
|
|
Send an HTTP reply. The Content-Length and Date headers are set
|
|
|
|
automatically.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
static void reply(IOutputStream*, CHTTPReply&);
|
|
|
|
|
2002-07-28 17:55:59 +00:00
|
|
|
//! Parse multipart form data
|
|
|
|
/*!
|
|
|
|
Parse a multipart/form-data body into its parts. Returns true
|
|
|
|
iff the entire body was correctly parsed.
|
|
|
|
*/
|
2002-05-30 16:13:16 +00:00
|
|
|
// FIXME -- name/value pairs insufficient to save part headers
|
|
|
|
static bool parseFormData(const CHTTPRequest&,
|
2002-06-10 22:06:45 +00:00
|
|
|
CFormParts& parts);
|
2002-05-30 16:13:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static CString readLine(IInputStream*, CString& tmpBuffer);
|
|
|
|
static CString readBlock(IInputStream*,
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32 numBytes, CString& tmpBuffer);
|
2002-06-02 13:34:35 +00:00
|
|
|
static CString readChunk(IInputStream*, CString& tmpBuffer,
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32* maxSize);
|
2002-05-30 16:13:16 +00:00
|
|
|
static void readHeaders(IInputStream*,
|
2002-06-10 22:06:45 +00:00
|
|
|
CHTTPRequest*, bool isFooter,
|
|
|
|
CString& tmpBuffer,
|
|
|
|
UInt32* maxSize);
|
2002-05-30 16:13:16 +00:00
|
|
|
|
|
|
|
static bool isValidToken(const CString&);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|