Annotation of parser3/src/include/pa_common.h, revision 1.135
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.135 ! misha 11: static const char * const IDENT_COMMON_H="$Date: 2009-08-30 05:28:41 $";
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"
1.134 misha 22: #define HTTP_STATUS "Status"
23: #define HTTP_CONTENT_LENGTH "Content-Length"
24:
1.127 misha 25: #define HTTP_CONTENT_TYPE "Content-Type"
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.134 misha 31: #define CONTENT_TRANSFER_ENCODING_NAME "Content-Transfer-Encoding"
32:
33: #define CONTENT_DISPOSITION "Content-Disposition"
34: #define CONTENT_DISPOSITION_LOWER "content-disposition"
35: #define CONTENT_DISPOSITION_ATTACHMENT "attachment"
36: #define CONTENT_DISPOSITION_INLINE "inline"
37: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
38:
1.135 ! misha 39: const String http_content_type(HTTP_CONTENT_TYPE);
! 40:
1.134 misha 41: const String content_transfer_encoding_name(CONTENT_TRANSFER_ENCODING_NAME);
42:
43: const String content_disposition(CONTENT_DISPOSITION);
44: const String content_disposition_inline(CONTENT_DISPOSITION_INLINE);
45: const String content_disposition_attachment(CONTENT_DISPOSITION_ATTACHMENT);
46: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
47:
48:
1.132 misha 49: #define HASH_ORDER
50:
51: #ifdef HASH_ORDER
52: #undef PA_HASH_CLASS
53: #include "pa_hash.h"
54: #endif
55:
1.87 paf 56: class Value;
1.132 misha 57: typedef HASH_STRING<Value*> HashStringValue;
1.64 paf 58:
59: // replace system s*nprintf with our versions
1.69 paf 60: #undef vsnprintf
1.93 paf 61: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 62: #define vsnprintf __vsnprintf
63: #undef snprintf
1.93 paf 64: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 65: #define snprintf __snprintf
1.63 paf 66:
1.21 paf 67: #if _MSC_VER
1.56 paf 68: /*
1.93 paf 69: inline int open( const char* filename, int oflag ) { return _open(filename, oflag); }
1.56 paf 70: inline int close( int handle ) { return _close(handle); }
71: inline int read( int handle, void *buffer, unsigned int count ) { return _read(handle,buffer,count); }
72: inline int write( int handle, const void *buffer, unsigned int count ) { return _write(handle,buffer,count); }
1.93 paf 73: inline int stat( const char* path, struct _stat *buffer ) { return _stat(path, buffer); }
1.56 paf 74: inline long lseek( int handle, long offset, int origin ) { return _lseek(handle, offset, origin); }
75: */
1.17 paf 76:
77: //access
78: #define F_OK 0
79: #define X_OK 1
80: #define W_OK 2
81: #define R_OK 4
82:
1.20 paf 83: #ifndef strcasecmp
84: # define strcasecmp _stricmp
85: #endif
86: #ifndef strncasecmp
87: # define strncasecmp _strnicmp
88: #endif
89: #ifndef mkdir
90: # define mkdir(path, mode) _mkdir(path)
91: #endif
1.17 paf 92:
1.20 paf 93: #ifndef putenv
94: # define putenv _putenv
1.1 paf 95: #endif
1.2 paf 96:
1.17 paf 97: #endif
1.59 paf 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.25 paf 201: const String& file_spec,
1.93 paf 202: const char* data, size_t size,
1.58 paf 203: bool as_text,
1.67 paf 204: bool do_append=false);
1.26 paf 205:
206: /**
207: delete specified file
208: throws an exception in case of problems
209: */
1.96 paf 210: bool file_delete(const String& file_spec, bool fail_on_problem=true);
1.47 parser 211: /**
212: move specified file
213: throws an exception in case of problems
214: */
1.70 paf 215: void file_move(const String& old_spec, const String& new_spec);
1.27 paf 216:
1.93 paf 217: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82 paf 218: bool entry_exists(const String& file_spec);
1.107 paf 219: bool file_exist(const String& file_spec);
220: bool dir_exists(const String& file_spec);
221: const String* file_exist(const String& path, const String& name);
1.36 paf 222: bool file_executable(const String& file_spec);
1.37 paf 223:
1.48 parser 224: bool file_stat(const String& file_spec,
1.126 misha 225: size_t& rsize,
226: time_t& ratime,
227: time_t& rmtime,
228: time_t& rctime,
229: bool fail_on_read_problem=true);
1.15 paf 230:
231: /**
1.18 paf 232: scans for @a delim[default \n] in @a *row_ref,
233: @return piece of line before it or end of string, if no @a delim found
234: assigns @a *row_ref to point right after delimiter if there were one
235: or to zero if no @a delim were found.
1.15 paf 236: */
1.7 paf 237: char *getrow(char **row_ref,char delim='\n');
1.76 paf 238: char *lsplit(char *string, char delim);
1.7 paf 239: char *lsplit(char **string_ref,char delim);
1.8 paf 240: char *rsplit(char *string, char delim);
1.93 paf 241: const char* format(double value, char *fmt);
1.10 paf 242:
1.30 paf 243: size_t stdout_write(const void *buf, size_t size);
1.14 paf 244:
1.124 misha 245: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool ignore_plus=false);
1.23 paf 246:
247: #ifdef WIN32
248: void back_slashes_to_slashes(char *s);
1.35 paf 249: //void slashes_to_back_slashes(char *s);
1.23 paf 250: #endif
251:
1.28 paf 252: #ifndef _qsort
253: # define _qsort(names,cnt,sizeof_names,func_addr) \
254: qsort(names,cnt,sizeof_names,func_addr)
255: #endif
1.34 paf 256:
1.119 misha 257: bool StrStartFromNC(const char* str, const char* substr, bool equal=false);
1.121 misha 258: size_t strpos(const char *str, const char *substr);
259:
1.133 misha 260: Charset* detect_charset(const char* content_type);
1.45 parser 261:
262: #define SECS_PER_DAY (60*60*24)
263: int getMonthDays(int year, int month);
1.52 parser 264:
1.116 misha 265: int remove_crlf(char *start, char *end);
1.90 paf 266:
1.93 paf 267: #ifdef PA_SAFE_MODE
268: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
269: #endif
1.99 paf 270:
1.108 misha 271: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104 paf 272: char* pa_base64_encode(const char *in, size_t in_size);
1.112 misha 273: struct File_base64_action_info {
274: unsigned char** base64;
275: };
276: char* pa_base64_encode(const String& file_spec);
277: static void file_base64_file_action(
1.126 misha 278: struct stat& finfo,
279: int f,
280: const String&, const char* /*fname*/, bool,
281: void *context);
1.108 misha 282:
1.109 misha 283: #define FILE_BUFFER_SIZE 4096
1.110 misha 284: static unsigned long crc32Table[256];
1.108 misha 285: static void InitCrc32Table()
286: {
1.110 misha 287: if(crc32Table[1] == 0){
1.108 misha 288: // This is the official polynomial used by CRC32 in PKZip.
289: // Often times the polynomial shown reversed as 0x04C11DB7.
290: static const unsigned long dwPolynomial = 0xEDB88320;
291:
292: for(int i = 0; i < 256; i++)
293: {
294: unsigned long dwCrc = i;
295: for(int j = 8; j > 0; j--)
296: {
297: if(dwCrc & 1)
298: dwCrc = (dwCrc >> 1) ^ dwPolynomial;
299: else
300: dwCrc >>= 1;
301: }
1.110 misha 302: crc32Table[i] = dwCrc;
1.108 misha 303: }
304: }
305: }
306:
1.111 misha 307: int file_block_read(const int f, unsigned char* buffer, const size_t size);
308:
1.108 misha 309: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
310: {
1.110 misha 311: crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)];
1.108 misha 312: }
313:
314: const unsigned long pa_crc32(const char *in, size_t in_size);
315: const unsigned long pa_crc32(const String& file_spec);
316: static void file_crc32_file_action(
1.126 misha 317: struct stat& finfo,
318: int f,
319: const String&, const char* /*fname*/, bool,
320: void *context);
1.93 paf 321:
1.109 misha 322: static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {
323: char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];
324: unsigned char *src=bytes;
325: unsigned char *end=bytes+size;
326: char *dest=bytes_hex;
327:
328: const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";
329:
330: for(; src<end; src++) {
1.126 misha 331: *dest++=hex[*src/0x10];
332: *dest++=hex[*src%0x10];
1.109 misha 333: }
334: *dest=0;
335:
336: return bytes_hex;
337: }
338:
1.106 paf 339: int pa_get_valid_file_options_count(HashStringValue& options);
340:
1.115 misha 341: // some stuff for use with .for_each
342: static void copy_all_overwrite_to(
1.126 misha 343: HashStringValue::key_type key,
344: HashStringValue::value_type value,
345: HashStringValue* dest) {
1.115 misha 346: dest->put(key, value);
347: }
348:
349: static void remove_key_from(
350: HashStringValue::key_type key,
351: HashStringValue::value_type /*value*/,
352: HashStringValue* dest) {
353: dest->remove(key);
354: }
355:
1.117 misha 356: static String::C date_gmt_string(tm* tms) {
357: /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
358: static const char month_names[12][4]={
359: "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
360: static const char days[7][4]={
361: "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
362:
363: char *buf=new(PointerFreeGC) char[MAX_STRING];
364: return String::C(buf,
365: snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
366: days[tms->tm_wday],
367: tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
368: tms->tm_hour,tms->tm_min,tms->tm_sec));
369: }
370:
1.115 misha 371:
1.93 paf 372: // globals
373:
374: extern const String file_status_name;
1.1 paf 375:
1.103 paf 376: // global defines for file options which are handled but not checked elsewhere, we check them
377:
378: #define PA_SQL_LIMIT_NAME "limit"
379: #define PA_SQL_OFFSET_NAME "offset"
380: #define PA_COLUMN_SEPARATOR_NAME "separator"
381: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113 misha 382: #define PA_CHARSET_NAME "charset"
1.103 paf 383:
1.115 misha 384: // globals defines for sql options
385:
386: #define SQL_BIND_NAME "bind"
387: #define SQL_DEFAULT_NAME "default"
388: #define SQL_DISTINCT_NAME "distinct"
389: #define SQL_VALUE_TYPE_NAME "type"
390:
391: #ifndef DOXYGEN
392: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
393: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
394: #endif
395:
1.1 paf 396: #endif
1.115 misha 397:
E-mail: