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