Annotation of parser3/src/include/pa_common.h, revision 1.159
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.146 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.74 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6: */
7:
8: #ifndef PA_COMMON_H
9: #define PA_COMMON_H
1.78 paf 10:
1.159 ! moko 11: #define IDENT_PA_COMMON_H "$Id: pa_common.h,v 1.158 2015/04/21 22:12:27 moko Exp $"
1.1 paf 12:
1.33 paf 13: #include "pa_string.h"
1.93 paf 14: #include "pa_hash.h"
1.1 paf 15:
1.128 misha 16: class Request;
17:
1.120 misha 18: // defines
1.136 misha 19: #define HTTP_USER_AGENT "user-agent"
1.140 misha 20:
1.136 misha 21: #define HTTP_STATUS "status"
1.140 misha 22: #define HTTP_STATUS_CAPITALIZED "Status"
23:
1.136 misha 24: #define HTTP_CONTENT_LENGTH "content-length"
1.140 misha 25: #define HTTP_CONTENT_LENGTH_CAPITALIZED "Content-Length"
1.136 misha 26:
1.159 ! moko 27: #define HTTP_CONTENT_TYPE "content-type"
! 28: #define HTTP_CONTENT_TYPE_UPPER "CONTENT-TYPE"
1.138 misha 29: #define HTTP_CONTENT_TYPE_CAPITALIZED "Content-Type"
1.127 misha 30: #define HTTP_CONTENT_TYPE_FORM_URLENCODED "application/x-www-form-urlencoded"
31: #define HTTP_CONTENT_TYPE_MULTIPART_FORMDATA "multipart/form-data"
32: #define HTTP_CONTENT_TYPE_MULTIPART_RELATED "multipart/related"
33: #define HTTP_CONTENT_TYPE_MULTIPART_MIXED "multipart/mixed"
1.120 misha 34:
1.159 ! moko 35: #define CONTENT_DISPOSITION "content-disposition"
! 36: #define CONTENT_DISPOSITION_UPPER "CONTENT-DISPOSITION"
1.138 misha 37: #define CONTENT_DISPOSITION_CAPITALIZED "Content-Disposition"
1.134 misha 38: #define CONTENT_DISPOSITION_ATTACHMENT "attachment"
39: #define CONTENT_DISPOSITION_INLINE "inline"
40: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
41:
1.145 misha 42: #define BASE64_STRICT_OPTION_NAME "strict"
43:
1.134 misha 44: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
45:
1.132 misha 46: #define HASH_ORDER
47:
48: #ifdef HASH_ORDER
49: #undef PA_HASH_CLASS
50: #include "pa_hash.h"
51: #endif
52:
1.87 paf 53: class Value;
1.132 misha 54: typedef HASH_STRING<Value*> HashStringValue;
1.64 paf 55:
56: // replace system s*nprintf with our versions
1.69 paf 57: #undef vsnprintf
1.93 paf 58: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 59: #define vsnprintf __vsnprintf
60: #undef snprintf
1.93 paf 61: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 62: #define snprintf __snprintf
1.63 paf 63:
1.153 moko 64: #ifdef _MSC_VER
1.17 paf 65:
66: //access
67: #define F_OK 0
68: #define X_OK 1
69: #define W_OK 2
70: #define R_OK 4
71:
1.20 paf 72: #ifndef strcasecmp
73: # define strcasecmp _stricmp
74: #endif
1.2 paf 75:
1.157 moko 76: #ifndef strncasecmp
77: # define strncasecmp _strnicmp
78: #endif
79:
1.17 paf 80: #endif
1.59 paf 81:
1.154 moko 82: /**
83: file related functions
84: */
1.136 misha 85:
1.154 moko 86: #define FILE_BUFFER_SIZE 4096
1.95 paf 87:
88: int pa_lock_shared_blocking(int fd);
89: int pa_lock_exclusive_blocking(int fd);
90: int pa_lock_exclusive_nonblocking(int fd);
91: int pa_unlock(int fd);
1.101 paf 92:
93: void create_dir_for_file(const String& file_spec);
1.93 paf 94:
1.154 moko 95: int pa_get_valid_file_options_count(HashStringValue& options);
96:
1.93 paf 97: typedef void (*File_read_action)(
1.126 misha 98: struct stat& finfo,
99: int f,
100: const String& file_spec, const char* fname, bool as_text,
101: void *context);
1.83 paf 102:
103: /**
104: shared-lock specified file,
105: do actions under lock.
106: if fail_on_read_problem is true[default] throws an exception
107:
108: @returns true if read OK
109: */
1.93 paf 110: bool file_read_action_under_lock(const String& file_spec,
111: const char* action_name, File_read_action action, void *context,
1.83 paf 112: bool as_text=false,
113: bool fail_on_read_problem=true);
1.128 misha 114:
1.15 paf 115: /**
1.93 paf 116: read specified text file using
1.15 paf 117: if fail_on_read_problem is true[default] throws an exception
1.83 paf 118:
1.93 paf 119: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.15 paf 120: */
1.93 paf 121: char *file_read_text(Request_charsets& charsets,
1.126 misha 122: const String& file_spec,
123: bool fail_on_read_problem=true,
124: HashStringValue* options=0,
125: bool transcode_result=true);
1.93 paf 126:
1.128 misha 127: char *file_load_text(Request& r,
128: const String& file_spec,
129: bool fail_on_read_problem=true,
130: HashStringValue* options=0,
131: bool transcode_result=true);
132:
1.93 paf 133: struct File_read_result {
134: bool success;
135: char* str; size_t length;
136: HashStringValue* headers;
137: };
1.17 paf 138:
139: /**
1.93 paf 140: read specified file using
1.29 paf 141: if fail_on_read_problem is true[default] throws an exception
1.83 paf 142:
1.93 paf 143: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.128 misha 144: */
145: File_read_result file_read(Request_charsets& charsets,
146: const String& file_spec,
147: bool as_text,
148: HashStringValue* options=0,
149: bool fail_on_read_problem=true,
150: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.93 paf 151:
1.128 misha 152: File_read_result file_load(Request& r,
153: const String& file_spec,
1.126 misha 154: bool as_text,
155: HashStringValue* options=0,
156: bool fail_on_read_problem=true,
157: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.83 paf 158:
159: typedef void (*File_write_action)(int f, void *context);
1.29 paf 160:
161: /**
1.71 paf 162: lock specified file exclusively,
163: do actions under lock.
164: throws an exception in case of problems
1.72 paf 165:
166: if block=false does non-blocking lock
167: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 168: */
1.72 paf 169: bool file_write_action_under_lock(
1.71 paf 170: const String& file_spec,
1.114 misha 171: const char* action_name,
172: File_write_action action,
173: void *context,
1.71 paf 174: bool as_text=false,
1.72 paf 175: bool do_append=false,
1.77 paf 176: bool do_block=true,
177: bool fail_on_lock_problem=true);
1.71 paf 178:
179: /**
180: write data to specified file,
1.17 paf 181: throws an exception in case of problems
182: */
1.70 paf 183: void file_write(
1.137 misha 184: Request_charsets& charsets,
1.25 paf 185: const String& file_spec,
1.137 misha 186: const char* data,
187: size_t size,
1.58 paf 188: bool as_text,
1.137 misha 189: bool do_append=false,
190: Charset* asked_charset=0);
1.26 paf 191:
192: /**
193: delete specified file
194: throws an exception in case of problems
195: */
1.149 misha 196: bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
1.47 parser 197: /**
198: move specified file
199: throws an exception in case of problems
200: */
1.149 misha 201: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
1.27 paf 202:
1.93 paf 203: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82 paf 204: bool entry_exists(const String& file_spec);
1.107 paf 205: bool file_exist(const String& file_spec);
206: bool dir_exists(const String& file_spec);
207: const String* file_exist(const String& path, const String& name);
1.36 paf 208: bool file_executable(const String& file_spec);
1.37 paf 209:
1.48 parser 210: bool file_stat(const String& file_spec,
1.126 misha 211: size_t& rsize,
212: time_t& ratime,
213: time_t& rmtime,
214: time_t& rctime,
215: bool fail_on_read_problem=true);
1.15 paf 216:
1.154 moko 217: size_t stdout_write(const void *buf, size_t size);
218:
219: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
220:
221: int file_block_read(const int f, unsigned char* buffer, const size_t size);
222:
223: /**
224: String related functions
225: */
226:
227: const char* capitalize(const char* s);
228:
229: /** under WIN32 "t" mode fixes DOS chars OK,
230: can't say that about other systems/ line break styles
1.15 paf 231: */
1.154 moko 232: void fix_line_breaks(char *str, size_t& length /* < may change! used to speedup next actions */);
233:
1.7 paf 234: char *getrow(char **row_ref,char delim='\n');
1.76 paf 235: char *lsplit(char *string, char delim);
1.7 paf 236: char *lsplit(char **string_ref,char delim);
1.8 paf 237: char *rsplit(char *string, char delim);
1.151 moko 238: const char* format(double value, const char *fmt);
1.10 paf 239:
1.142 misha 240: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool js=false/*true==decode \uXXXX and don't convert '+' to space*/);
1.23 paf 241:
1.148 misha 242: char *search_stop(char*& current, char cstop_at);
243:
1.157 moko 244: inline int pa_strncasecmp(const char* str, const char* substr, size_t count=0) {
245: return strncasecmp(str, substr, count ? count : strlen(substr));
246: }
247:
1.23 paf 248: #ifdef WIN32
249: void back_slashes_to_slashes(char *s);
250: #endif
251:
1.121 misha 252: size_t strpos(const char *str, const char *substr);
253:
1.116 misha 254: int remove_crlf(char *start, char *end);
1.90 paf 255:
1.144 misha 256: inline bool pa_isalpha(unsigned char c) {
257: return (((c>='A') && (c<='Z')) || ((c>='a') && (c<='z')));
258: }
259:
260: inline bool pa_isalnum(unsigned char c) {
261: return (((c>='0') && (c<='9')) || pa_isalpha(c));
262: }
263:
1.154 moko 264: const char* hex_string(unsigned char* bytes, size_t size, bool upcase);
1.155 moko 265: extern const char* hex_digits;
1.99 paf 266:
1.145 misha 267: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size, bool strict=false);
1.104 paf 268: char* pa_base64_encode(const char *in, size_t in_size);
1.112 misha 269: char* pa_base64_encode(const String& file_spec);
1.108 misha 270:
1.158 moko 271: const char *pa_idna_encode(const char *in, Charset &source);
272: const char *pa_idna_decode(const char *in, Charset &source);
273:
1.108 misha 274: const unsigned long pa_crc32(const char *in, size_t in_size);
275: const unsigned long pa_crc32(const String& file_spec);
1.93 paf 276:
1.154 moko 277: /**
278: Mix functions
279: */
1.106 paf 280:
1.115 misha 281: // some stuff for use with .for_each
282: static void copy_all_overwrite_to(
1.126 misha 283: HashStringValue::key_type key,
284: HashStringValue::value_type value,
285: HashStringValue* dest) {
1.115 misha 286: dest->put(key, value);
287: }
288:
289: static void remove_key_from(
290: HashStringValue::key_type key,
291: HashStringValue::value_type /*value*/,
292: HashStringValue* dest) {
293: dest->remove(key);
294: }
295:
1.154 moko 296: Charset* detect_charset(const char* content_type);
297:
298: #define SECS_PER_DAY (60*60*24)
299:
300: int getMonthDays(int year, int month);
301:
302: String::C date_gmt_string(tm* tms);
1.117 misha 303:
1.93 paf 304: // globals
305:
306: extern const String file_status_name;
1.1 paf 307:
1.103 paf 308: // global defines for file options which are handled but not checked elsewhere, we check them
309:
310: #define PA_SQL_LIMIT_NAME "limit"
311: #define PA_SQL_OFFSET_NAME "offset"
312: #define PA_COLUMN_SEPARATOR_NAME "separator"
313: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113 misha 314: #define PA_CHARSET_NAME "charset"
1.150 moko 315: #define PA_RESPONSE_CHARSET_NAME "response-charset"
1.103 paf 316:
1.115 misha 317: // globals defines for sql options
318:
319: #define SQL_BIND_NAME "bind"
320: #define SQL_DEFAULT_NAME "default"
321: #define SQL_DISTINCT_NAME "distinct"
322: #define SQL_VALUE_TYPE_NAME "type"
323:
324: #ifndef DOXYGEN
325: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
326: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
327: #endif
328:
1.1 paf 329: #endif
1.115 misha 330:
E-mail: