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

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

E-mail: