1
// ---------------------------------------------------------------------------
3
// - afnix:nwg module - http reply class implementation -
4
// ---------------------------------------------------------------------------
5
// - This program is free software; you can redistribute it and/or modify -
6
// - it provided that this copyright notice is kept intact. -
8
// - This program is distributed in the hope that it will be useful, but -
9
// - without any warranty; without even the implied warranty of -
10
// - merchantability or fitness for a particular purpose. In no event shall -
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
// - special damages arising in any way out of the use of this software. -
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2007 amaury darsch -
15
// ---------------------------------------------------------------------------
18
#include "Runnable.hpp"
19
#include "HttpReply.hpp"
20
#include "QuarkZone.hpp"
21
#include "Exception.hpp"
25
// -------------------------------------------------------------------------
26
// - private section -
27
// -------------------------------------------------------------------------
29
// the content type header as a mime property
30
static const String HTTP_MIME_NAME = "Content-Type";
31
static const String HTTP_MIME_PVAL = "text/plain";
33
// the http location name
34
static const String HTTP_RLOC_NAME = "Location";
35
// the http cookie version 0 name
36
static const String HTTP_COK0_NAME = "Set-Cookie";
37
// the http cookie version 1 name
38
static const String HTTP_COK1_NAME = "Set-Cookie2";
40
// the http status name
41
static const String HTTP_STAT_NAME = "Status";
43
// this procedure returns true if the header contains a status code that
44
// prohibits the existence of a message body
45
static bool is_body_valid (const Plist& head) {
46
// check if the status property is defined
47
if (head.exists (HTTP_STAT_NAME) == false) return true;
48
// get the status value and check
49
String mesg = head.getpval (HTTP_STAT_NAME);
50
if (mesg == HttpProto::mapcode (100)) return false;
51
if (mesg == HttpProto::mapcode (101)) return false;
52
if (mesg == HttpProto::mapcode (204)) return false;
53
if (mesg == HttpProto::mapcode (304)) return false;
57
// -------------------------------------------------------------------------
59
// -------------------------------------------------------------------------
61
// create an empty http reply
63
HttpReply::HttpReply (void) {
64
// add the content-type property
65
sethead (HTTP_MIME_NAME, HTTP_MIME_PVAL);
68
// create an empty http reply with a type
70
HttpReply::HttpReply (const String& type) {
71
sethead (HTTP_MIME_NAME, type);
74
// return the class name
76
String HttpReply::repr (void) const {
80
// set a cookie in the header
82
void HttpReply::setcook (const Cookie& cook) {
85
if (cook.getvers () == 0) {
86
sethead (HTTP_COK0_NAME, cook.tostring ());
87
} else if (cook.getvers () == 1) {
88
sethead (HTTP_COK1_NAME, cook.tostring ());
90
throw Exception ("http-error", "invalid cookie version");
99
// add a string in the http buffer
101
void HttpReply::addhbuf (const Literal& lval) {
104
d_hbuf.add (lval.tostring ());
112
// add a mime document in the http buffer
114
void HttpReply::addhbuf (const Mime& mime) {
118
String type = mime.getmime ();
119
sethead (HTTP_MIME_NAME, type);
120
// while the mime dcoument to the http buffer
129
// add a buffer in the http buffer
131
void HttpReply::addhbuf (const Buffer& buf) {
142
// set a http status by code
144
void HttpReply::setstatus (const long code) {
147
// get the status message
148
sethead (HTTP_STAT_NAME, HttpProto::mapcode (code));
156
// redirect a to a certain location
158
void HttpReply::redirect (const String& url) {
161
// set a status 303 (see other)
163
// set the redirection location
164
sethead (HTTP_RLOC_NAME, url);
172
// write the http reply to an output stream
174
void HttpReply:: write (Output& os) const {
178
HttpProto::write (os);
180
if (is_body_valid (d_head) == true) {
191
// write the http reply to a buffer
193
void HttpReply:: write (Buffer& buf) const {
197
HttpProto::write (buf);
199
if (is_body_valid (d_head) == true) {
210
// -------------------------------------------------------------------------
211
// - object section -
212
// -------------------------------------------------------------------------
215
static const long QUARK_ZONE_LENGTH = 4;
216
static QuarkZone zone (QUARK_ZONE_LENGTH);
218
// the object supported quarks
219
static const long QUARK_SETCOOK = zone.intern ("set-cookie");
220
static const long QUARK_ADDHBUF = zone.intern ("add-buffer");
221
static const long QUARK_REDIRECT = zone.intern ("redirect");
222
static const long QUARK_SETSTATUS = zone.intern ("set-status");
224
// create a new object in a generic way
226
Object* HttpReply::mknew (Vector* argv) {
227
long argc = (argv == nilp) ? 0 : argv->length ();
228
// check for 0 argument
229
if (argc == 0) return new HttpReply;
230
//check for 1 argument
232
String type = argv->getstring (0);
233
return new HttpReply (type);
236
throw Exception ("argument-error",
237
"too many arguments with http reply constructor");
240
// return true if the given quark is defined
242
bool HttpReply::isquark (const long quark, const bool hflg) const {
244
if (zone.exists (quark) == true) {
248
bool result = hflg ? HttpProto::isquark (quark, hflg) : false;
253
// apply this object with a set of arguments and a quark
255
Object* HttpReply::apply (Runnable* robj, Nameset* nset, const long quark,
257
// get the number of arguments
258
long argc = (argv == nilp) ? 0 : argv->length ();
260
// dispatch 1 argument
262
if (quark == QUARK_ADDHBUF) {
263
Object* obj = argv->get (0);
264
// check for a literal
265
Literal* lobj = dynamic_cast <Literal*> (obj);
270
// check for a buffer
271
Buffer* buf = dynamic_cast <Buffer*> (obj);
276
// check for a mime document
277
Mime* mobj = dynamic_cast <Mime*> (obj);
282
throw Exception ("type-error", "invalid object with add-buffer",
285
if (quark == QUARK_REDIRECT) {
286
String url = argv->getstring (0);
290
if (quark == QUARK_SETSTATUS) {
291
long code = argv->getint (0);
295
if (quark == QUARK_SETCOOK) {
296
Object* obj = argv->get (0);
297
Cookie* cobj = dynamic_cast <Cookie*> (obj);
299
throw Exception ("type-error", "invalid object with set-cookie",
306
// call the http proto method
307
return HttpProto::apply (robj, nset, quark, argv);