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