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

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

E-mail: