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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/**
* @file response.c
*
* @brief Implementation of response_t.
*
*/
/*
* Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#include "response.h"
#include <stdlib.h>
#include <stdarg.h>
#include <utils/linked_list.h>
typedef struct {
char *name;
char *value;
} name_value_t;
/**
* create name value pair
*/
static name_value_t *name_value_create(char *name, char *value)
{
name_value_t *this = malloc_thing(name_value_t);
this->name = strdup(name);
this->value = strdup(value);
return this;
}
/**
* destroy a name value pair
*/
static void name_value_destroy(name_value_t *this)
{
free(this->name);
free(this->value);
free(this);
}
typedef struct private_response_t private_response_t;
/**
* private data of the task manager
*/
struct private_response_t {
/**
* public functions
*/
response_t public;
/**
* the associated fcgi request
*/
FCGX_Request *req;
/**
* Content type
*/
char *content_type;
/**
* list of cookies (name_value_t)
*/
linked_list_t *cookies;
/**
* list of custom headers (name_value_t)
*/
linked_list_t *headers;
/**
* headers already written?
*/
bool started;
};
/**
* write the headers, if not already written
*/
static void write_headers(private_response_t *this)
{
iterator_t *iterator;
name_value_t *current;
FCGX_FPrintF(this->req->out, "Content-type: %s\n", this->content_type);
iterator = this->cookies->create_iterator(this->cookies, TRUE);
while (iterator->iterate(iterator, (void**)¤t))
{
FCGX_FPrintF(this->req->out, "Set-Cookie: %s=%s; path=%s\n",
current->name, current->value,
FCGX_GetParam("SCRIPT_NAME", this->req->envp));
}
iterator->destroy(iterator);
iterator = this->cookies->create_iterator(this->headers, TRUE);
while (iterator->iterate(iterator, (void**)¤t))
{
FCGX_FPrintF(this->req->out, "%s: %s\n",
current->name, current->value);
}
iterator->destroy(iterator);
FCGX_PutChar('\n', this->req->out);
this->started = TRUE;
}
/**
* Implementation of response_t.print.
*/
static void print_(private_response_t *this, char *str)
{
if (!this->started)
{
write_headers(this);
}
FCGX_PutS(str, this->req->out);
}
/**
* Implementation of response_t.printf.
*/
static void printf_(private_response_t *this, char *format, ...)
{
va_list args;
if (!this->started)
{
write_headers(this);
}
va_start(args, format);
FCGX_VFPrintF(this->req->out, format, args);
va_end(args);
}
/**
* Implementation of response_t.add_header.
*/
static void add_header(private_response_t *this, char *name, char *value)
{
this->headers->insert_last(this->headers, name_value_create(name, value));
}
/**
* Implementation of response_t.set_content_type.
*/
static void set_content_type(private_response_t *this, char *type)
{
free(this->content_type);
this->content_type = strdup(type);
}
/**
* Implementation of response_t.add_cookie.
*/
static void add_cookie(private_response_t *this, char *name, char *value)
{
this->cookies->insert_last(this->cookies, name_value_create(name, value));
}
/**
* Implementation of response_t.redirect.
*/
static void redirect(private_response_t *this, char *location)
{
FCGX_FPrintF(this->req->out, "Status: 303 See Other\n");
FCGX_FPrintF(this->req->out, "Location: %s%s%s\n\n",
FCGX_GetParam("SCRIPT_NAME", this->req->envp),
*location == '/' ? "" : "/", location);
}
/**
* Implementation of response_t.destroy
*/
static void destroy(private_response_t *this)
{
this->headers->destroy_function(this->headers, (void*)name_value_destroy);
this->cookies->destroy_function(this->cookies, (void*)name_value_destroy);
free(this->content_type);
free(this);
}
/*
* see header file
*/
response_t *response_create(FCGX_Request *request)
{
private_response_t *this = malloc_thing(private_response_t);
this->public.print = (void(*)(response_t*, char *str))print_;
this->public.printf = (void(*)(response_t*, char *format, ...))printf_;
this->public.add_header = (void(*)(response_t*, char *name, char *value))add_header;
this->public.set_content_type = (void(*)(response_t*, char *type))set_content_type;
this->public.add_cookie = (void(*)(response_t*, char *name, char *value))add_cookie;
this->public.redirect = (void(*)(response_t*, char *location))redirect;
this->public.destroy = (void(*)(response_t*))destroy;
this->req = request;
this->headers = linked_list_create();
this->cookies = linked_list_create();
this->content_type = strdup("text/html");
this->started = FALSE;
return &this->public;
}
|