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

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

E-mail: