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