File: httpd.h

package info (click to toggle)
madman 0.93.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,060 kB
  • ctags: 3,309
  • sloc: cpp: 14,194; ansic: 13,331; sh: 100; makefile: 62; python: 54
file content (187 lines) | stat: -rw-r--r-- 4,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
madman - a music manager
Copyright (C) 2003  Andreas Kloeckner <ak@ixion.net>

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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/




#ifndef _HEADER_SEEN_HTTPD_H
#define _HEADER_SEEN_HTTPD_H



#include <qhttp.h>
#include <qserversocket.h>
#include "utility/base.h"
#include <stdexcept>




class QSocket;
class QUrl;




struct tHttpException : public runtime_error
{
  int HttpStatusCode;

  tHttpException(int status_code, const QString &str)
    : runtime_error(str.latin1()), HttpStatusCode(status_code)
    { }
};




class tHttpResponseHeader : public QHttpHeader
{
  public:
    tHttpResponseHeader();
    tHttpResponseHeader(const tHttpResponseHeader& header);
    tHttpResponseHeader(int code, const QString& text = QString::null, int majorVer = 1, int minorVer = 1);
    tHttpResponseHeader(const QString& str);

    void setStatusLine(int code, const QString& text = QString::null, int majorVer = 1, int minorVer = 1);

    int statusCode() const;
    QString reasonPhrase() const;

    int majorVersion() const;
    int minorVersion() const;

    bool parseLine(const QString& line, int number);
    QString toString() const;

  private:
    int statCode;
    QString reasonPhr;
    int majVer;
    int minVer;
};




class tHttpResponder 
{
  public:
    virtual bool canHandle(const QUrl &url, QHttpRequestHeader &header) = 0;
    /** Handles the request and gives ownership of the socket to
     * a tHttpResponseSender, which will eventually destroy it.
     */
    virtual void handle(const QUrl &url, const QHttpRequestHeader &header, QSocket *&socket) = 0;
};




class tHttpResponseSender : public QObject
{
    Q_OBJECT;

  protected:
    QSocket *Socket;

  private:
    QCString Header;
    unsigned SizeOfSentHeader;
    bool CurrentlySending;
    bool AlreadyClosing;

  public:
    tHttpResponseSender();
    virtual ~tHttpResponseSender();
    virtual void setHeader(tHttpResponseHeader &header);
    virtual void readyToSend() = 0;
    void kickOff(QSocket *&socket);

  public slots:
    void readyToSendInternal(int dummy);
    void connectionClosed();
};




class tHttpStringResponseSender : public tHttpResponseSender
{
    QCString    Document;
    unsigned 	SizeOfSentDocument;

    typedef tHttpResponseSender super;

  public:
    tHttpStringResponseSender(const QCString &document);
    void setHeader(tHttpResponseHeader &header);
    void readyToSend();
};




/** This class is for internal use only.
 */
class tHttpRequestReceiver : public QObject
{
    Q_OBJECT;

    QSocket	*Socket;
    QString	HeadersSoFar;

  public:
    /** This class does not assume ownership of the QSocket.
     */
    tHttpRequestReceiver(QSocket *socket);
  private slots:
    void dataReady();
    void connectionClosed();

  signals:
    void requestReceived(QSocket *socket, const QString &headers);
};




class tHttpDaemon : public QServerSocket
{
    Q_OBJECT;

    typedef vector<tHttpResponder *> 	tResponderList;
    tResponderList			ResponderList;
    tHttpResponder			*DefaultResponder;
    bool				RestrictToLocalhost;

  public:
    tHttpDaemon(int port, bool restrict_to_localhost, QObject* parent=0);
    ~tHttpDaemon();
    void newConnection(int socket);
    void addResponder(tHttpResponder *responder);
    void setDefaultResponder(tHttpResponder *responder);

  private slots:
    void requestReceived(QSocket *socket, const QString &headers);
};




#endif