Annotation of parser3/src/include/pa_common.h, revision 1.139
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.130 misha 4: Copyright (c) 2001-2009 ArtLebedev Group (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.139 ! misha 11: static const char * const IDENT_COMMON_H="$Date: 2009-09-10 09:39:48 $";
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"
20: #define HTTP_STATUS "status"
21: #define HTTP_CONTENT_LENGTH "content-length"
22:
23: #define HTTP_CONTENT_TYPE "content-type"
1.127 misha 24: #define HTTP_CONTENT_TYPE_UPPER "CONTENT-TYPE"
1.138 misha 25: #define HTTP_CONTENT_TYPE_CAPITALIZED "Content-Type"
1.127 misha 26: #define HTTP_CONTENT_TYPE_FORM_URLENCODED "application/x-www-form-urlencoded"
27: #define HTTP_CONTENT_TYPE_MULTIPART_FORMDATA "multipart/form-data"
28: #define HTTP_CONTENT_TYPE_MULTIPART_RELATED "multipart/related"
29: #define HTTP_CONTENT_TYPE_MULTIPART_MIXED "multipart/mixed"
1.120 misha 30:
1.139 ! misha 31: #define CONTENT_TRANSFER_ENCODING_NAME "content-transfer-encoding"
! 32: #define CONTENT_TRANSFER_ENCODING_CAPITALIZED "Content-Transfer-Encoding"
1.134 misha 33:
1.136 misha 34: #define CONTENT_DISPOSITION "content-disposition"
1.138 misha 35: #define CONTENT_DISPOSITION_CAPITALIZED "Content-Disposition"
1.134 misha 36: #define CONTENT_DISPOSITION_ATTACHMENT "attachment"
37: #define CONTENT_DISPOSITION_INLINE "inline"
38: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
39:
1.135 misha 40: const String http_content_type(HTTP_CONTENT_TYPE);
41:
1.134 misha 42: const String content_transfer_encoding_name(CONTENT_TRANSFER_ENCODING_NAME);
43:
44: const String content_disposition(CONTENT_DISPOSITION);
45: const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
46: const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
47: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
48:
49:
1.132 misha 50: #define HASH_ORDER
51:
52: #ifdef HASH_ORDER
53: #undef PA_HASH_CLASS
54: #include "pa_hash.h"
55: #endif
56:
1.87 paf 57: class Value;
1.132 misha 58: typedef HASH_STRING<Value*> HashStringValue;
1.64 paf 59:
60: // replace system s*nprintf with our versions
1.69 paf 61: #undef vsnprintf
1.93 paf 62: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 63: #define vsnprintf __vsnprintf
64: #undef snprintf
1.93 paf 65: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 66: #define snprintf __snprintf
1.63 paf 67:
1.21 paf 68: #if _MSC_VER
1.56 paf 69: /*
1.93 paf 70: inline int open( const char* filename, int oflag ) { return _open(filename, oflag); }
1.56 paf 71: inline int close( int handle ) { return _close(handle); }
72: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
73: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
1.93 paf 74: inline int stat( const char* path, struct _stat *buffer ) { return _stat(path, buffer); }
1.56 paf 75: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
76: */
1.17 paf 77:
78: //access
79: #define F_OK 0
80: #define X_OK 1
81: #define W_OK 2
82: #define R_OK 4
83:
1.20 paf 84: #ifndef strcasecmp
85: # define strcasecmp _stricmp
86: #endif
87: #ifndef strncasecmp
88: # define strncasecmp _strnicmp
89: #endif
90: #ifndef mkdir
91: # define mkdir(path, mode) _mkdir(path)
92: #endif
1.17 paf 93:
1.20 paf 94: #ifndef putenv
95: # define putenv _putenv
1.1 paf 96: #endif
1.2 paf 97:
1.17 paf 98: #endif
1.59 paf 99:
1.136 misha 100: const char* capitalize(const char* s);
101:
1.54 parser 102: /** under WIN32 "t" mode fixes DOS chars OK,
103: can't say that about other systems/ line break styles
104: */
1.57 paf 105: void fix_line_breaks(
1.126 misha 106: char *str,
107: size_t& length///< may change! used to speedup next actions
108: );
1.95 paf 109:
110: int pa_lock_shared_blocking(int fd);
111: int pa_lock_exclusive_blocking(int fd);
112: int pa_lock_exclusive_nonblocking(int fd);
113: int pa_unlock(int fd);
1.101 paf 114:
115: void create_dir_for_file(const String& file_spec);
1.93 paf 116:
117: typedef void (*File_read_action)(
1.126 misha 118: struct stat& finfo,
119: int f,
120: const String& file_spec, const char* fname, bool as_text,
121: void *context);
1.83 paf 122:
123: /**
124: shared-lock specified file,
125: do actions under lock.
126: if fail_on_read_problem is true[default] throws an exception
127:
128: @returns true if read OK
129: */
1.93 paf 130: bool file_read_action_under_lock(const String& file_spec,
131: const char* action_name, File_read_action action, void *context,
1.83 paf 132: bool as_text=false,
133: bool fail_on_read_problem=true);
1.128 misha 134:
1.15 paf 135: /**
1.93 paf 136: read specified text file using
1.15 paf 137: if fail_on_read_problem is true[default] throws an exception
1.83 paf 138:
1.93 paf 139: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.15 paf 140: */
1.93 paf 141: char *file_read_text(Request_charsets& charsets,
1.126 misha 142: const String& file_spec,
143: bool fail_on_read_problem=true,
144: HashStringValue* options=0,
145: bool transcode_result=true);
1.93 paf 146:
1.128 misha 147: char *file_load_text(Request& r,
148: const String& file_spec,
149: bool fail_on_read_problem=true,
150: HashStringValue* options=0,
151: bool transcode_result=true);
152:
1.93 paf 153: struct File_read_result {
154: bool success;
155: char* str; size_t length;
156: HashStringValue* headers;
157: };
1.17 paf 158:
159: /**
1.93 paf 160: read specified file using
1.29 paf 161: if fail_on_read_problem is true[default] throws an exception
1.83 paf 162:
1.93 paf 163: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.128 misha 164: */
165: File_read_result file_read(Request_charsets& charsets,
166: const String& file_spec,
167: bool as_text,
168: HashStringValue* options=0,
169: bool fail_on_read_problem=true,
170: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.93 paf 171:
1.128 misha 172: File_read_result file_load(Request& r,
173: const String& file_spec,
1.126 misha 174: bool as_text,
175: HashStringValue* options=0,
176: bool fail_on_read_problem=true,
177: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.83 paf 178:
179: typedef void (*File_write_action)(int f, void *context);
1.29 paf 180:
181: /**
1.71 paf 182: lock specified file exclusively,
183: do actions under lock.
184: throws an exception in case of problems
1.72 paf 185:
186: if block=false does non-blocking lock
187: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 188: */
1.72 paf 189: bool file_write_action_under_lock(
1.71 paf 190: const String& file_spec,
1.114 misha 191: const char* action_name,
192: File_write_action action,
193: void *context,
1.71 paf 194: bool as_text=false,
1.72 paf 195: bool do_append=false,
1.77 paf 196: bool do_block=true,
197: bool fail_on_lock_problem=true);
1.71 paf 198:
199: /**
200: write data to specified file,
1.17 paf 201: throws an exception in case of problems
202: */
1.70 paf 203: void file_write(
1.137 misha 204: Request_charsets& charsets,
1.25 paf 205: const String& file_spec,
1.137 misha 206: const char* data,
207: size_t size,
1.58 paf 208: bool as_text,
1.137 misha 209: bool do_append=false,
210: Charset* asked_charset=0);
1.26 paf 211:
212: /**
213: delete specified file
214: throws an exception in case of problems
215: */
1.96 paf 216: bool file_delete(const String& file_spec, bool fail_on_problem=true);
1.47 parser 217: /**
218: move specified file
219: throws an exception in case of problems
220: */
1.70 paf 221: void file_move(const String& old_spec, const String& new_spec);
1.27 paf 222:
1.93 paf 223: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82 paf 224: bool entry_exists(const String& file_spec);
1.107 paf 225: bool file_exist(const String& file_spec);
226: bool dir_exists(const String& file_spec);
227: const String* file_exist(const String& path, const String& name);
1.36 paf 228: bool file_executable(const String& file_spec);
1.37 paf 229:
1.48 parser 230: bool file_stat(const String& file_spec,
1.126 misha 231: size_t& rsize,
232: time_t& ratime,
233: time_t& rmtime,
234: time_t& rctime,
235: bool fail_on_read_problem=true);
1.15 paf 236:
237: /**
1.18 paf 238: scans for @a delim[default \n] in @a *row_ref,
239: @return piece of line before it or end of string, if no @a delim found
240: assigns @a *row_ref to point right after delimiter if there were one
241: or to zero if no @a delim were found.
1.15 paf 242: */
1.7 paf 243: char *getrow(char **row_ref,char delim='\n');
1.76 paf 244: char *lsplit(char *string, char delim);
1.7 paf 245: char *lsplit(char **string_ref,char delim);
1.8 paf 246: char *rsplit(char *string, char delim);
1.93 paf 247: const char* format(double value, char *fmt);
1.10 paf 248:
1.30 paf 249: size_t stdout_write(const void *buf, size_t size);
1.14 paf 250:
1.124 misha 251: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool ignore_plus=false);
1.23 paf 252:
253: #ifdef WIN32
254: void back_slashes_to_slashes(char *s);
1.35 paf 255: //void slashes_to_back_slashes(char *s);
1.23 paf 256: #endif
257:
1.28 paf 258: #ifndef _qsort
259: # define _qsort(names,cnt,sizeof_names,func_addr) \
260: qsort(names,cnt,sizeof_names,func_addr)
261: #endif
1.34 paf 262:
1.119 misha 263: bool StrStartFromNC(const char* str, const char* substr, bool equal=false);
1.121 misha 264: size_t strpos(const char *str, const char *substr);
265:
1.133 misha 266: Charset* detect_charset(const char* content_type);
1.45 parser 267:
268: #define SECS_PER_DAY (60*60*24)
269: int getMonthDays(int year, int month);
1.52 parser 270:
1.116 misha 271: int remove_crlf(char *start, char *end);
1.90 paf 272:
1.93 paf 273: #ifdef PA_SAFE_MODE
274: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
275: #endif
1.99 paf 276:
1.108 misha 277: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104 paf 278: char* pa_base64_encode(const char *in, size_t in_size);
1.112 misha 279: struct File_base64_action_info {
280: unsigned char** base64;
281: };
282: char* pa_base64_encode(const String& file_spec);
283: static void file_base64_file_action(
1.126 misha 284: struct stat& finfo,
285: int f,
286: const String&, const char* /*fname*/, bool,
287: void *context);
1.108 misha 288:
1.109 misha 289: #define FILE_BUFFER_SIZE 4096
1.110 misha 290: static unsigned long crc32Table[256];
1.108 misha 291: static void InitCrc32Table()
292: {
1.110 misha 293: if(crc32Table[1] == 0){
1.108 misha 294: // This is the official polynomial used by CRC32 in PKZip.
295: // Often times the polynomial shown reversed as 0x04C11DB7.
296: static const unsigned long dwPolynomial = 0xEDB88320;
297:
298: for(int i = 0; i < 256; i++)
299: {
300: unsigned long dwCrc = i;
301: for(int j = 8; j > 0; j--)
302: {
303: if(dwCrc & 1)
304: dwCrc = (dwCrc >> 1) ^ dwPolynomial;
305: else
306: dwCrc >>= 1;
307: }
1.110 misha 308: crc32Table[i] = dwCrc;
1.108 misha 309: }
310: }
311: }
312:
1.111 misha 313: int file_block_read(const int f, unsigned char* buffer, const size_t size);
314:
1.108 misha 315: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
316: {
1.110 misha 317: crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)];
1.108 misha 318: }
319:
320: const unsigned long pa_crc32(const char *in, size_t in_size);
321: const unsigned long pa_crc32(const String& file_spec);
322: static void file_crc32_file_action(
1.126 misha 323: struct stat& finfo,
324: int f,
325: const String&, const char* /*fname*/, bool,
326: void *context);
1.93 paf 327:
1.109 misha 328: static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {
329: char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];
330: unsigned char *src=bytes;
331: unsigned char *end=bytes+size;
332: char *dest=bytes_hex;
333:
334: const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";
335:
336: for(; src<end; src++) {
1.126 misha 337: *dest++=hex[*src/0x10];
338: *dest++=hex[*src%0x10];
1.109 misha 339: }
340: *dest=0;
341:
342: return bytes_hex;
343: }
344:
1.106 paf 345: int pa_get_valid_file_options_count(HashStringValue& options);
346:
1.115 misha 347: // some stuff for use with .for_each
348: static void copy_all_overwrite_to(
1.126 misha 349: HashStringValue::key_type key,
350: HashStringValue::value_type value,
351: HashStringValue* dest) {
1.115 misha 352: dest->put(key, value);
353: }
354:
355: static void remove_key_from(
356: HashStringValue::key_type key,
357: HashStringValue::value_type /*value*/,
358: HashStringValue* dest) {
359: dest->remove(key);
360: }
361:
1.117 misha 362: static String::C date_gmt_string(tm* tms) {
363: /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
364: static const char month_names[12][4]={
365: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
366: static const char days[7][4]={
367: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
368:
369: char *buf=new(PointerFreeGC) char[MAX_STRING];
370: return String::C(buf,
371: snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
372: days[tms->tm_wday],
373: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
374: tms->tm_hour,tms->tm_min,tms->tm_sec));
375: }
376:
1.115 misha 377:
1.93 paf 378: // globals
379:
380: extern const String file_status_name;
1.1 paf 381:
1.103 paf 382: // global defines for file options which are handled but not checked elsewhere, we check them
383:
384: #define PA_SQL_LIMIT_NAME "limit"
385: #define PA_SQL_OFFSET_NAME "offset"
386: #define PA_COLUMN_SEPARATOR_NAME "separator"
387: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113 misha 388: #define PA_CHARSET_NAME "charset"
1.103 paf 389:
1.115 misha 390: // globals defines for sql options
391:
392: #define SQL_BIND_NAME "bind"
393: #define SQL_DEFAULT_NAME "default"
394: #define SQL_DISTINCT_NAME "distinct"
395: #define SQL_VALUE_TYPE_NAME "type"
396:
397: #ifndef DOXYGEN
398: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
399: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
400: #endif
401:
1.1 paf 402: #endif
1.115 misha 403:
E-mail: