Annotation of parser3/src/include/pa_sapi.h, revision 1.35
1.1 paf 1: /** @file
2: Parser: web server api interface object decl.
3:
1.33 moko 4: Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)
1.18 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_SAPI_H
9: #define PA_SAPI_H
1.20 paf 10:
1.35 ! moko 11: #define IDENT_PA_SAPI_H "$Id: pa_sapi.h,v 1.34 2020/08/13 11:44:20 moko Exp $"
1.23 paf 12:
13: // includes
14:
1.35 ! moko 15: #include "pa_common.h"
1.23 paf 16: #include "pa_array.h"
1.35 ! moko 17: #include "pa_exception.h"
1.1 paf 18:
1.23 paf 19: // forwards
20: class SAPI_Info;
1.5 paf 21:
1.4 paf 22: /// target web-Server API
1.1 paf 23: struct SAPI {
1.5 paf 24: /// log error message
1.23 paf 25: static void log(SAPI_Info& info, const char* fmt, ...);
26: /// log error message & exit
27: static void die(const char* fmt, ...);
1.1 paf 28: /// read POST request bytes
1.23 paf 29: static size_t read_post(SAPI_Info& info, char *buf, size_t max_bytes);
1.12 parser 30: /// add response header attribute [but do not send it to client]
1.28 misha 31: static void add_header_attribute(SAPI_Info& info, const char* dont_store_key, const char* dont_store_value);
1.12 parser 32: /// send collected header attributes to client
1.23 paf 33: static void send_header(SAPI_Info& info);
1.1 paf 34: /// output body bytes
1.26 paf 35: static size_t send_body(SAPI_Info& info, const void *buf, size_t size);
1.30 moko 36:
1.35 ! moko 37: static void send_error(SAPI_Info& info, const char *exception_cstr, const char *status = "500"){
! 38: // capitalized headers passed for preventing malloc during capitalization
! 39: add_header_attribute(info, HTTP_STATUS_CAPITALIZED, status);
! 40: add_header_attribute(info, HTTP_CONTENT_TYPE_CAPITALIZED, "text/plain");
! 41: send_header(info);
! 42: send_body(info, exception_cstr, strlen(exception_cstr));
! 43: }
! 44:
1.30 moko 45: class Env {
46: public:
47: /// entire environment
48: static const char* const* get(SAPI_Info& ainfo);
49: /// single environment string
50: static char* get(SAPI_Info& ainfo, const char* name);
51:
52: class Iterator {
53: private:
54: const char* const* pairs;
55: const char* pair;
56: const char* eq_at;
57: public:
1.32 moko 58: Iterator(SAPI_Info& asapi_info) : pair(NULL), eq_at(NULL){
1.30 moko 59: if(pairs=SAPI::Env::get(asapi_info))
60: next();
61: }
62: operator bool () {
63: return pair!=0;
64: }
65: void next() {
66: while(pair=*pairs++)
67: if(eq_at=strchr(pair, '=')) // valid pair (key=value)
68: if(eq_at[1]) // value is not empty
69: break;
70: }
71: char* key(){
72: return pa_strdup(pair, eq_at-pair);
73: }
74: char* value(){
75: return pa_strdup(eq_at+1);
76: }
77: };
78: };
1.1 paf 79: };
80:
81: #endif
E-mail: