forked from xaxaxa/workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.H
More file actions
775 lines (761 loc) · 24.5 KB
/
page.H
File metadata and controls
775 lines (761 loc) · 24.5 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
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, see <http://www.gnu.org/licenses/>.
* */
/*
* page.H
*
* Created on: Jan 26, 2013
* Author: xaxaxa
*/
#ifndef PAGE_H_
#define PAGE_H_
#include <rgc.H>
#include <map>
#include <set>
#include <cpoll/cpoll.H>
#include <string>
#include "stringutils.H"
#include "headercontainer.H"
#include "common.H"
#define CPPSP_VERSION 20130705
using namespace std;
using CP::String;
using CP::AsyncValue;
namespace cppsp
{
class cppspManager;
class loadedPage;
class staticPage;
class Host;
class HTTPException: public std::exception
{
public:
int code;
char tmp[5];
HTTPException();
HTTPException(int32_t number);
HTTPException(string message, int32_t number = 0);
~HTTPException() throw ();
const char* what() const throw ();
};
/**
Represents an HTTP request and contains data from the request such as HTTP headers, querystrings, and HTTP POST data.
*/
class Request: public RGC::Object
{
public:
Request(CP::Stream& inp, CP::StringPool* sp);
void init(CP::Stream& inp, CP::StringPool* sp);
virtual ~Request() {
}
/**
internal field do not use.
*/
RGC::Ref<CP::Stream> inputStream;
/**
The per-request StringPool instance. You can allocate memory from this if it only need to persist for the duration of this http request. The StringPool is cleared after the page has finished executing.
*/
CP::StringPool* sp; //may be used to store headers, querystrings, and POST data
typedef map<String, String, less<String>, CP::PoolAllocator<std::pair<const String, String> > > StringMap;
CP::PoolAllocator<std::pair<const String, String> > alloc;
/**
HTTP request headers.
*/
headerContainer headers;
/**
Query strings.
*/
StringMap queryString;
/**
POST data.
*/
StringMap form;
Server* server;
/**
can be used to pass custom parameters into a page (from a request router, etc)
*/
void* customParams;
/**
HTTP method (GET, POST, etc).
*/
String method;
/**
HTTP request path (directly from the HTTP header; not file path; does not include querystrings).
*/
String path;
//string httpVersion;
/**
You don't need to call this manually; POST data is automatically read for all HTTP POST requests.
*/
virtual void parsePost() {
}
/**
internal method do not use.
*/
void doParsePost(String buf);
/**
internal method do not use.
See Response::reset().
*/
virtual void reset();
};
/**
Represents an HTTP response.
*/
class Response: public RGC::Object
{
public:
typedef Delegate<void(Response&)> Callback;
Response(CP::Stream& out, CP::StringPool* sp);
Response(const Response& other) = delete;
void init(CP::Stream& out, CP::StringPool* sp);
/**
internal field do not use.
*/
RGC::Ref<CP::Stream> outputStream;
/**
internal field do not use.
*/
CP::MemoryStream buffer;
/**
You can write data to the HTTP response body by writing to "output". Content-Length will be automatically calculated.
*/
CP::StreamWriter output;
/**
internal field do not use.
*/
Callback flushCB;
/**
The per-request StringPool instance. You can allocate memory from this if it only need to persist for the duration of this http request. The StringPool is cleared after the page has finished executing.
*/
CP::StringPool* sp;
//headerContainer2 headers;
CP::PoolAllocator<std::pair<const String, String> > alloc;
typedef map<String, String, less<String>, CP::PoolAllocator<std::pair<const String, String> > > StringMap;
/**
HTTP response headers.
If you want to append your own headers, you can:
1. in init(), set headersExtraSpace to the maximum number of bytes you think you'll append
2. append your headers past the end of this string
3. update the length of this string
OR
1. after init(), make a copy of headers (appending your own data into it in the process);
when allocating space for your new string, allocate N extra bytes at the end, where N
is strlen("Content-Length: 0000000000\r\n")
2. update headers to point to your new string; headers.len should be set to the total length
of the actual headers, not including the extra space at the end
3. set _headersEnd to NULL.
If you want to _replace_ the whole header string with your own, the same over-allocation
rule applies.
*/
String headers;
/**
HTTP response status (OK, Internal Server Error, etc).
*/
String statusName;
char* _headersEnd;
/**
internal field do not use.
*/
iovec iov[3];
/**
HTTP response status code (200, 500, etc).
*/
int statusCode;
/**
Extra space to allocate (in bytes) for the headers string
*/
int headersExtraSpace;
/**
internal field do not use.
*/
int _bufferPos;
int _writeTo;
/**
internal field do not use.
*/
bool headersWritten;
/**
internal field do not use.
*/
bool closed;
/**
TODO: chunked output not yet implemented.
*/
bool sendChunked;
bool keepAlive;
bool _writing;
bool _doWrite;
/*virtual void doWriteHeaders();
void writeHeaders() {
if (!headersWritten) {
doWriteHeaders();
headersWritten = true;
}
}*/
/**
internal method do not use.
*/
virtual void flush(bool finalize = false);
virtual void finalize(Callback cb);
/**
clear buffered output
*/
virtual void clear();
/**
internal method do not use.
*/
void addDefaultHeaders(String time, String mime);
/**
Serialize the HTTP response headers.
*/
void serializeHeaders(CP::StreamWriter& sw);
/**
Write data to the HTTP response body.
*/
template<class ... T>
void write(T&&... a) {
output.write(std::forward<T>(a)...);
}
void appendHeaders(String h);
/**
internal method do not use.
*/
void _writeCB(int r);
/**
internal method do not use.
Partially destructs the Response object. You can call init()
later on to reuse this Response object. This violates RAII
principles, but is necessary for good performance and low
memory usage.
*/
virtual void reset();
};
class Page;
/**
Represents a request handler callback.
*/
typedef DelegateRef<void(Request&, Response&, Delegate<void()> cb)> Handler;
typedef DelegateChain<void(Request&, Response&, Delegate<void()>)> HandleRequestChain;
typedef DelegateChain<void(Request&, Response&, exception& ex, Delegate<void()>)> HandleErrorChain;
struct ModuleInstance
{
loadedPage* origin;
void* instance;
bool operator<(const ModuleInstance& other) const {
if (origin < other.origin) return true;
if (origin > other.origin) return false;
return instance < other.instance;
}
bool operator==(const ModuleInstance& other) const {
return origin == other.origin && instance == other.instance;
}
ModuleInstance() {
}
ModuleInstance(loadedPage* origin, void* instance) :
origin(origin), instance(instance) {
}
ModuleInstance(nullptr_t) :
origin(nullptr), instance(nullptr) {
}
};
class ModuleContainer
{
public:
set<ModuleInstance> modules;
struct moduleOrigin
{
loadedPage* origin;
set<void*> modules;
};
map<string, set<ModuleInstance> > moduleMap_fileName;
map<string, set<ModuleInstance> > moduleMap_path;
map<string, set<ModuleInstance> > moduleMap_name;
/**
Find all inserted modules with a file name of fn.
@param fn the file name (not path) e.g. module1.cppsm
*/
set<ModuleInstance>& findModulesByFileName(string fn);
/**
Find all inserted modules with a path of p.
@param p the path.
*/
set<ModuleInstance>& findModulesByPath(string p);
/**
Find all inserted modules with a module name (not filename) of n.
A module can declare its name by declaring a getModuleInfo(ModuleInfo&) function
and setting the "name" field in the ModuleInfo to the desired name in that function.
@param n the module name.
*/
set<ModuleInstance>& findModulesByName(string n);
/**
see findModulesByFileName().
@returns the first item in the set. The ordering of the set is undefined, so only use this if you are sure the result set only contains one item.
*/
ModuleInstance findByFileName(string fn);
/**
see findModulesByPath().
@returns the first item in the set. The ordering of the set is undefined, so only use this if you are sure the result set only contains one item.
*/
ModuleInstance findByPath(string p);
/**
see findModulesByName().
@returns the first item in the set. The ordering of the set is undefined, so only use this if you are sure the result set only contains one item.
*/
ModuleInstance findByName(string n);
/**
Same as findByName()
*/
ModuleInstance operator[](string name) {
return findByName(name);
}
set<ModuleInstance>::iterator begin() {
return modules.begin();
}
set<ModuleInstance>::iterator end() {
return modules.end();
}
ModuleInstance insertModule(loadedPage* lp, ModuleParams& mp);
void removeModule(ModuleInstance inst);
};
struct PerformanceCounters
{ //to be incremented by the web server implementation
int totalRequestsReceived;
int totalRequestsFinished;
PerformanceCounters() :
totalRequestsReceived(0), totalRequestsFinished(0) {
}
};
class Host: public RGC::Object
{
public:
typedef DelegateChain<Server*(Request&)> PreRouteRequestChain;
CP::Poll* poll;
PerformanceCounters performanceCounters;
set<Server*> servers;
ModuleContainer modules;
timespec curTime { 0, 0 }; //CLOCK_MONOTONIC
timespec curClockTime { 0, 0 }; //CLOCK_REALTIME
String curRFCTime;
int threadID;
//timer interval when the web server is active
int timerShortInterval = 2;
//timer interval when the web server is idle (no requests received since the last timer tick)
int timerLongInterval = 120;
Server* defaultServer;
Host();
~Host();
void addServer(Server* srv);
void removeServer(Server* srv);
Server* defaultPreRouteRequest(Request& req);
PreRouteRequestChain preRouteRequest;
/**
should be called during every timer tick
@returns whether or not the timer still need to run
*/
virtual bool updateTime(bool noCleanCache = false);
/**
Returns the cppspManager instance for this Host instance; may be NULL if the underlying server does not use cppspManager.
*/
virtual cppspManager* manager() {
return NULL;
}
/**
Load static file from "path". path is absolute.
*/
virtual AsyncValue<Handler> routeStaticRequest(String path)=0;
/**
Load dynamic page from "path". path is absolute.
*/
virtual AsyncValue<Handler> routeDynamicRequest(String path)=0;
virtual AsyncValue<loadedPage*> loadPage(String path)=0;
/**
@param fd whether to keep the file descriptor open
@param map whether to map the file into virtual memory
*/
virtual staticPage* loadStaticPage(String path, bool fd = false, bool map = true)=0;
/**
Load cppsp module from "path". path is absolute.
*/
virtual AsyncValue<ModuleInstance> loadModule(String path);
/**
Instantiate and insert a loaded module into this Host instance.
*/
virtual ModuleInstance insertModule(loadedPage* lp);
/**
Remove an inserted module instance from this Host instance.
*/
virtual void removeModule(ModuleInstance inst);
};
class DefaultHost: public Host
{
public:
cppspManager* mgr;
string compilerWorkingDirectory;
/**
Time (in seconds) before considering purging a cached cppspManager managed page.
For static pages, each cache entry contains an mmap()ed area backed by that file.
For dynamic pages, each cache entry contains an mmap()ed area backed by the page's string
table, and a dynamic linker handle from dlopen().
*/
int fileCacheCleanInterval = 600; //10 min.
DefaultHost();
~DefaultHost();
bool updateTime(bool noCleanCache = false) override;
bool cleanCache();
cppspManager* manager() override;
AsyncValue<loadedPage*> loadPage(String path) override;
staticPage* loadStaticPage(String path, bool fd = false, bool map = true) override;
};
/**
Represents a cppsp web server instance.
*/
class Server: public RGC::Object
{
public:
Host* host;
string root;
PerformanceCounters performanceCounters;
ModuleContainer modules;
string defaultMime;
CP::Poll* poll;
int threadID;
Server();
~Server();
inline timespec curTime() {
return host->curTime;
}
inline timespec curClockTime() {
return host->curClockTime;
}
inline String curRFCTime() {
return host->curRFCTime;
}
/**
The handle request callback. It is called for every request right after reading in all the request data and instantiating the Request and Response objects.
You can override the behavior of the web server by attaching (from within a cppsp module). See the example .cppsm modules in /www (included with the cppsp source).
*/
HandleRequestChain handleRequest;
/**
The error handler callback. Called when an exception occurs during the execution of a request handler, request router, or page.
*/
HandleErrorChain handleError;
/**
Load static file from "path" and serve it to the specified response. path is relative to server root.
*/
void handleStaticRequest(String path, Request& req, Response& resp, Delegate<void()> cb) {
return handleStaticRequestFromFile(mapPath(path, *req.sp), req, resp, cb);
}
/**
Load static file from "path" and serve it to the specified response. path is absolute.
*/
virtual void handleStaticRequestFromFile(String path, Request& req, Response& resp,
Delegate<void()> cb);
/**
Load dynamic page from "path" and serve it to the specified response. path is relative to server root.
*/
void handleDynamicRequest(String path, Request& req, Response& resp, Delegate<void()> cb) {
return handleDynamicRequestFromFile(mapPath(path, *req.sp), req, resp, cb);
}
/**
Load dynamic page from "path" and serve it to the specified response. path is absolute.
*/
virtual void handleDynamicRequestFromFile(String path, Request& req, Response& resp,
Delegate<void()> cb);
/**
Calls routeRequest() to obtain the handler for the specific uri, and caches the
routing decision.
*/
void handleRoutedRequest(String path, Request& req, Response& resp, Delegate<void()> cb);
/**
Default request handler. Calls handleRoutedRequest() with req.path as the path.
*/
void defaultHandleRequest(Request& req, Response& resp, Delegate<void()> cb);
/**
Default error handler. Outputs the "Server Error" page similar to ASP.net
*/
void defaultHandleError(Request& req, Response& resp, exception& ex, Delegate<void()> cb);
/*
Called by the default request handler to translate a request uri to a handler delegate.
The purpose of attaching a request router instead of a request handler directly to "handleRequest" is so that
the routing decision can be cached.
@returns a handler delegate that will be used to process requests destined for that uri. If the return value is undefined, it means the request is being completed asynchornously.
*/
DelegateChain<AsyncValue<Handler>(String path)> routeRequest;
/**
Load static file from "path". path is relative to server root.
*/
AsyncValue<Handler> routeStaticRequest(String path) {
return routeStaticRequestFromFile(mapPath(path.toSTDString()));
}
/**
Load static file from "path". path is absolute.
*/
AsyncValue<Handler> routeStaticRequestFromFile(String path) {
return host->routeStaticRequest(path);
}
/**
Load dynamic page from "path". path is relative to server root.
*/
AsyncValue<Handler> routeDynamicRequest(String path) {
return routeDynamicRequestFromFile(mapPath(path.toSTDString()));
}
/**
Load dynamic page from "path". path is absolute.
*/
AsyncValue<Handler> routeDynamicRequestFromFile(String path) {
return host->routeDynamicRequest(path);
}
/**
Default request router. Calls either routeStaticRequest() or routeDynamicRequest() based on
the filename extension. Files ending with .cppsp are treated as dynamic pages, and everything else
are treated as static pages.
*/
AsyncValue<Handler> defaultRouteRequest(String path);
struct RouteCacheEntry
{
Handler handler;
string path;
timespec lastUpdate; //CLOCK_MONOTONIC
};
/**
Internal field.
Holds route cache entries.
*/
map<String, RouteCacheEntry*> routeCache;
/**
Time (in seconds) before considering a cached routing decision invalid and perform a re-route.
Increasing this may increase performance, but will increase the delay before a page is recompiled
after being edited.
*/
int routeCacheDuration = 2;
/**
Time (in seconds) before considering purging a routing entry from the routeCache map.
Although routes expire after routeCacheDuration seconds, deleting and reinserting the same key
into a map is expensive, so we keep the entry itself around for longer by default.
Increasing this may increase performance, but may increase memory usage.
*/
int routeCacheCleanInterval = 120;
/**
Returns absolute path of server root directory.
*/
String rootDir() {
return root;
}
/**
Load and instantiate dynamic page from "path". path is relative to server root.
*/
virtual AsyncValue<Page*> loadPage(String path, RGC::Allocator& a);
/**
Load and instantiate dynamic page from "path". path is absolute.
*/
virtual AsyncValue<Page*> loadPageFromFile(String path, RGC::Allocator& a);
/**
Load cppsp module from "path". path is relative to server root.
*/
virtual AsyncValue<ModuleInstance> loadModule(String path);
/**
Load cppsp module from "path". path is absolute.
*/
virtual AsyncValue<ModuleInstance> loadModuleFromFile(String path);
virtual ModuleInstance insertModule(loadedPage* lp);
virtual void removeModule(ModuleInstance inst);
/**
Load static page from "path". path is relative to server root.
*/
virtual String loadStaticPage(String path);
/**
Load static page from "path". path is absolute.
*/
virtual String loadStaticPageFromFile(String path);
/**
Given a path relative to the server root, returns the absolute path.
*/
virtual string mapPath(string path);
/**
Given a path relative to the server root, returns the absolute path. The returned string is allocated from "a".
*/
virtual String mapPath(String path, RGC::Allocator& a);
/**
Returns the cppspManager instance for this Server instance; may be NULL if the underlying server does not use cppspManager.
*/
virtual cppspManager* manager();
/**
should be called every time after you update Host::curTime or Host::curClockTime
@returns whether or not the timer still need to run
*/
virtual bool updateTime(bool noCleanCache = false);
timespec _lastClean { 0, 0 };
/**
Purges expired cache items.
@returns whether or not there are items left that could be purged in the future.
*/
virtual bool cleanCache(int minAge);
virtual bool cleanCache() {
return cleanCache(routeCacheCleanInterval);
}
};
/**
Base class for all cppsp pages.
*/
class Page: public RGC::Object
{
public:
typedef Delegate<void()> Callback;
Page(Request& req, Response& resp, CP::StringPool* sp);
/**
Object representing the HTTP request.
*/
RGC::Ref<Request> request;
/**
Object representing the HTTP response.
*/
RGC::Ref<Response> response;
RGC::Ref<cppsp::loadedPage> lp;
/**
Poll instance for the current thread. Can be used for asynchronous I/O, timers, etc.
*/
CP::Poll* poll;
/**
The current Server instance. Usually there is one Server instance for every thread.
*/
Server* server;
/**
internal field do not use.
web server callback.
*/
Callback cb;
/**
Absolute path of the current cppsp page.
*/
String filePath;
/**
The per-request StringPool instance. You can allocate memory from this if it only need to persist for the duration of this http request. The StringPool is cleared after the page has finished executing.
*/
CP::StringPool* sp;
bool doRender;
bool doReadPost;
Page() :
doRender(true) {
}
inline void cancelRender() {
doRender = false;
}
/**
internal field do not use. Pointer to mmap()ed address of the string table file.
*/
const uint8_t* __stringTable;
/**
internal method do not use.
*/
void __writeStringTable(int i, int len);
/**
Given a path relative to the current page, returns the absolute path. The returned string is allocated from the request-wide StringPool and is invalid after the page finishes executing.
*/
String mapPath(String path);
/**
Given a path relative to the current page, returns the absolute path. The returned string is allocated from "a".
*/
String mapPath(String path, RGC::Allocator& a);
/**
Given a path relative to the current page, returns the path relative to the server root. The returned string is allocated from the request-wide StringPool and is invalid after the page finishes executing.
*/
String mapRelativePath(String path);
/**
Given a path relative to the current page, returns the path relative to the server root. The returned string is allocated from "a".
*/
String mapRelativePath(String path, RGC::Allocator& a);
/**
Load and instantiate dynamic page from "path". path is relative to the current page.
*/
AsyncValue<Page*> loadNestedPage(String path);
/**
Load and instantiate dynamic page from "path". path is relative to the current page. Returned Page object is allocated from "a".
*/
AsyncValue<Page*> loadNestedPage(String path, RGC::Allocator& a);
/**
Load and instantiate dynamic page from "path". path is absolute.
*/
AsyncValue<Page*> loadNestedPageFromFile(String path);
/**
Load and instantiate dynamic page from "path". path is absolute. Returned Page object is allocated from "a".
*/
AsyncValue<Page*> loadNestedPageFromFile(String path, RGC::Allocator& a);
/**
Load static page from "path". path is relative to the current page.
*/
String loadNestedStaticPage(String path);
/**
Load static page from "path". path is absolute.
*/
String loadNestedStaticPageFromFile(String path);
//perform SSI from "path". path is relative to the current page.
void staticInclude(String path);
//perform SSI from "path". path is absolute.
void staticIncludeFromFile(String path);
/**
Called by the web server immediately after instantiating the current page. The page calls "cb" when it is done processing, to signal the web server that it can clean up the StringPool and all request-related data. The object pointed to by fields "request" and "response" may be recycled.
*/
virtual void handleRequest(Callback cb = nullptr);
virtual void processRequest() {
doInit();
}
/**
Each page goes through these stages (in order): init, [readPost], load, render, flush, finalize, [web server callback], ~Page
To be overridden by base class; called before load() is called, and must call initCB() or cancelLoad() after it's done
*/
virtual void init() {
}
virtual void doInit();
void prepareResponse() {
response->addDefaultHeaders(server->host->curRFCTime, server->defaultMime);
}
virtual void preRespond() {}
virtual void startResponse();
void initCB() {
preRespond();
prepareResponse();
startResponse();
}
virtual void cancelLoad(exception* ex = NULL);
/**
To be overridden by derived class; called before headers are written. See init() for a description of the request life-cycle.
*/
virtual void load() {
}
virtual void render(CP::StreamWriter& out);
/**
Called after rendering. See init() for a description of the request life-cycle.
*/
virtual void flush();
/**
Called by flush() after it is done writing. See init() for a description of the request life-cycle.
*/
virtual void flushCB() {
finalize();
}
/**
Called by flushCB. See init() for a description of the request life-cycle.
*/
virtual void finalize() {
finalizeCB();
}
/**
Called by finalize() after it is done doing its work. This will call the web server's callback and cause it to clean-up and re-cycle the Request and Response objects, and destroy the Page object. See init() for a description of the request life-cycle.
*/
virtual void finalizeCB();
virtual ~Page() {
}
void _flushCB(Response& r);
};
} /* namespace cppsp */
#endif /* PAGE_H_ */