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

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.141   ! pretende   11: static const char * const IDENT_COMMON_H="$Date: 2009-10-06 11:39: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"
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.124     misha     255: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool ignore_plus=false);
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.93      paf       277: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
1.99      paf       278: 
1.108     misha     279: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size);
1.104     paf       280: char* pa_base64_encode(const char *in, size_t in_size);
1.112     misha     281: struct File_base64_action_info {
                    282:        unsigned char** base64;
                    283: }; 
                    284: char* pa_base64_encode(const String& file_spec);
                    285: static void file_base64_file_action(
1.126     misha     286:                                struct stat& finfo, 
                    287:                                int f, 
                    288:                                const String&, const char* /*fname*/, bool, 
                    289:                                void *context);
1.108     misha     290: 
1.109     misha     291: #define FILE_BUFFER_SIZE       4096
1.110     misha     292: static unsigned long crc32Table[256];
1.108     misha     293: static void InitCrc32Table()
                    294: {
1.110     misha     295:        if(crc32Table[1] == 0){
1.108     misha     296:                // This is the official polynomial used by CRC32 in PKZip.
                    297:                // Often times the polynomial shown reversed as 0x04C11DB7.
                    298:                static const unsigned long dwPolynomial = 0xEDB88320;
                    299: 
                    300:                for(int i = 0; i < 256; i++)
                    301:                {
                    302:                        unsigned long dwCrc = i;
                    303:                        for(int j = 8; j > 0; j--)
                    304:                        {
                    305:                                if(dwCrc & 1)
                    306:                                        dwCrc = (dwCrc >> 1) ^ dwPolynomial;
                    307:                                else
                    308:                                        dwCrc >>= 1;
                    309:                        }
1.110     misha     310:                        crc32Table[i] = dwCrc;
1.108     misha     311:                }
                    312:        }
                    313: }
                    314: 
1.111     misha     315: int file_block_read(const int f, unsigned char* buffer, const size_t size);
                    316: 
1.108     misha     317: inline void CalcCrc32(const unsigned char byte, unsigned long &crc32)
                    318: {
1.110     misha     319:        crc32 = ((crc32) >> 8) ^ crc32Table[(byte) ^ ((crc32) & 0x000000FF)];
1.108     misha     320: }
                    321: 
                    322: const unsigned long pa_crc32(const char *in, size_t in_size);
                    323: const unsigned long pa_crc32(const String& file_spec);
                    324: static void file_crc32_file_action(
1.126     misha     325:                                struct stat& finfo, 
                    326:                                int f, 
                    327:                                const String&, const char* /*fname*/, bool, 
                    328:                                void *context);
1.93      paf       329: 
1.109     misha     330: static const char* hex_string(unsigned char* bytes, size_t size, bool upcase) {
                    331:        char *bytes_hex=new(PointerFreeGC) char [size*2/*byte->hh*/+1/*for zero-teminator*/];
                    332:        unsigned char *src=bytes;
                    333:        unsigned char *end=bytes+size;
                    334:        char *dest=bytes_hex;
                    335: 
                    336:        const char *hex=upcase?"0123456789ABCDEF":"0123456789abcdef";
                    337: 
                    338:        for(; src<end; src++) {
1.126     misha     339:                *dest++=hex[*src/0x10];
                    340:                *dest++=hex[*src%0x10];
1.109     misha     341:        }
                    342:        *dest=0;
                    343: 
                    344:        return bytes_hex;
                    345: }
                    346: 
1.106     paf       347: int pa_get_valid_file_options_count(HashStringValue& options);
                    348: 
1.115     misha     349: // some stuff for use with .for_each
                    350: static void copy_all_overwrite_to(
1.126     misha     351:                                                                HashStringValue::key_type key, 
                    352:                                                                HashStringValue::value_type value, 
                    353:                                                                HashStringValue* dest) {
1.115     misha     354:        dest->put(key, value);
                    355: }
                    356: 
                    357: static void remove_key_from(
                    358:                                                        HashStringValue::key_type key, 
                    359:                                                        HashStringValue::value_type /*value*/, 
                    360:                                                        HashStringValue* dest) {
                    361:        dest->remove(key);
                    362: }
                    363: 
1.117     misha     364: static String::C date_gmt_string(tm* tms) {
                    365:        /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
                    366:        static const char month_names[12][4]={
                    367:                "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
                    368:        static const char days[7][4]={
                    369:                "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
                    370: 
                    371:        char *buf=new(PointerFreeGC) char[MAX_STRING];
                    372:        return String::C(buf, 
                    373:                snprintf(buf, MAX_STRING, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT", 
                    374:                days[tms->tm_wday],
                    375:                tms->tm_mday,month_names[tms->tm_mon],tms->tm_year+1900,
                    376:                tms->tm_hour,tms->tm_min,tms->tm_sec));
                    377: }
                    378: 
1.115     misha     379: 
1.93      paf       380: // globals
                    381: 
                    382: extern const String file_status_name;
1.1       paf       383: 
1.103     paf       384: // global defines for file options which are handled but not checked elsewhere, we check them
                    385: 
                    386: #define PA_SQL_LIMIT_NAME "limit"
                    387: #define PA_SQL_OFFSET_NAME "offset"
                    388: #define PA_COLUMN_SEPARATOR_NAME "separator"
                    389: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113     misha     390: #define PA_CHARSET_NAME "charset"
1.103     paf       391: 
1.115     misha     392: // globals defines for sql options
                    393: 
                    394: #define SQL_BIND_NAME "bind"
                    395: #define SQL_DEFAULT_NAME "default"
                    396: #define SQL_DISTINCT_NAME "distinct"
                    397: #define SQL_VALUE_TYPE_NAME "type"
                    398: 
                    399: #ifndef DOXYGEN
                    400: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
                    401: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
                    402: #endif
                    403: 
1.1       paf       404: #endif
1.115     misha     405: 

E-mail: