Annotation of parser3/src/include/pa_common.h, revision 1.138

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

E-mail: