Annotation of parser3/src/include/pa_common.h, revision 1.164
1.15 paf 1: /** @file
1.16 paf 2: Parser: commonly used functions.
3:
1.161 moko 4: Copyright (c) 2001-2015 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.164 ! moko 11: #define IDENT_PA_COMMON_H "$Id: pa_common.h,v 1.163 2016/07/21 18:30:10 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.164 ! moko 19: #define HTTP_STATUS "status"
1.140 misha 20: #define HTTP_STATUS_CAPITALIZED "Status"
21:
1.164 ! moko 22: #define HTTP_CONTENT_LENGTH "content-length"
1.140 misha 23: #define HTTP_CONTENT_LENGTH_CAPITALIZED "Content-Length"
1.136 misha 24:
1.159 moko 25: #define HTTP_CONTENT_TYPE "content-type"
26: #define HTTP_CONTENT_TYPE_UPPER "CONTENT-TYPE"
1.138 misha 27: #define HTTP_CONTENT_TYPE_CAPITALIZED "Content-Type"
1.120 misha 28:
1.159 moko 29: #define CONTENT_DISPOSITION "content-disposition"
30: #define CONTENT_DISPOSITION_UPPER "CONTENT-DISPOSITION"
1.138 misha 31: #define CONTENT_DISPOSITION_CAPITALIZED "Content-Disposition"
1.164 ! moko 32:
! 33: #define CONTENT_DISPOSITION_ATTACHMENT "attachment"
! 34: #define CONTENT_DISPOSITION_INLINE "inline"
1.134 misha 35: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
36:
1.164 ! moko 37: #define HTTP_CONTENT_TYPE_FORM_URLENCODED "application/x-www-form-urlencoded"
! 38: #define HTTP_CONTENT_TYPE_MULTIPART_FORMDATA "multipart/form-data"
! 39: #define HTTP_CONTENT_TYPE_MULTIPART_RELATED "multipart/related"
! 40: #define HTTP_CONTENT_TYPE_MULTIPART_MIXED "multipart/mixed"
! 41:
1.145 misha 42: #define BASE64_STRICT_OPTION_NAME "strict"
43:
1.134 misha 44: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
45:
1.132 misha 46: #define HASH_ORDER
47:
48: #ifdef HASH_ORDER
49: #undef PA_HASH_CLASS
50: #include "pa_hash.h"
51: #endif
52:
1.87 paf 53: class Value;
1.132 misha 54: typedef HASH_STRING<Value*> HashStringValue;
1.64 paf 55:
56: // replace system s*nprintf with our versions
1.69 paf 57: #undef vsnprintf
1.93 paf 58: int __vsnprintf(char *, size_t, const char* , va_list);
1.64 paf 59: #define vsnprintf __vsnprintf
60: #undef snprintf
1.93 paf 61: int __snprintf(char *, size_t, const char* , ...);
1.64 paf 62: #define snprintf __snprintf
1.63 paf 63:
1.153 moko 64: #ifdef _MSC_VER
1.17 paf 65:
66: //access
67: #define F_OK 0
68: #define X_OK 1
69: #define W_OK 2
70: #define R_OK 4
71:
1.20 paf 72: #ifndef strcasecmp
73: # define strcasecmp _stricmp
74: #endif
1.2 paf 75:
1.157 moko 76: #ifndef strncasecmp
77: # define strncasecmp _strnicmp
78: #endif
79:
1.17 paf 80: #endif
1.59 paf 81:
1.154 moko 82: /**
83: file related functions
84: */
1.136 misha 85:
1.154 moko 86: #define FILE_BUFFER_SIZE 4096
1.95 paf 87:
88: int pa_lock_shared_blocking(int fd);
89: int pa_lock_exclusive_blocking(int fd);
90: int pa_lock_exclusive_nonblocking(int fd);
91: int pa_unlock(int fd);
1.101 paf 92:
93: void create_dir_for_file(const String& file_spec);
1.93 paf 94:
1.154 moko 95: int pa_get_valid_file_options_count(HashStringValue& options);
96:
1.163 moko 97: typedef void (*File_read_action)(struct stat& finfo, int f, const String& file_spec, void *context);
1.83 paf 98:
99: /**
100: shared-lock specified file,
101: do actions under lock.
102: if fail_on_read_problem is true[default] throws an exception
103:
104: @returns true if read OK
105: */
1.93 paf 106: bool file_read_action_under_lock(const String& file_spec,
107: const char* action_name, File_read_action action, void *context,
1.83 paf 108: bool as_text=false,
109: bool fail_on_read_problem=true);
1.128 misha 110:
1.15 paf 111: /**
1.93 paf 112: read specified text file using
1.15 paf 113: if fail_on_read_problem is true[default] throws an exception
1.83 paf 114:
1.93 paf 115: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.15 paf 116: */
1.93 paf 117: char *file_read_text(Request_charsets& charsets,
1.126 misha 118: const String& file_spec,
119: bool fail_on_read_problem=true,
120: HashStringValue* options=0,
121: bool transcode_result=true);
1.93 paf 122:
1.128 misha 123: char *file_load_text(Request& r,
124: const String& file_spec,
125: bool fail_on_read_problem=true,
126: HashStringValue* options=0,
127: bool transcode_result=true);
128:
1.93 paf 129: struct File_read_result {
130: bool success;
131: char* str; size_t length;
132: HashStringValue* headers;
133: };
1.17 paf 134:
135: /**
1.93 paf 136: read specified file using
1.29 paf 137: if fail_on_read_problem is true[default] throws an exception
1.83 paf 138:
1.93 paf 139: WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.128 misha 140: */
141: File_read_result file_read(Request_charsets& charsets,
142: const String& file_spec,
143: bool as_text,
144: HashStringValue* options=0,
145: bool fail_on_read_problem=true,
146: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.93 paf 147:
1.128 misha 148: File_read_result file_load(Request& r,
149: const String& file_spec,
1.126 misha 150: bool as_text,
151: HashStringValue* options=0,
152: bool fail_on_read_problem=true,
153: char* buf=0, size_t offset=0, size_t size=0, bool transcode_text_result=true);
1.83 paf 154:
155: typedef void (*File_write_action)(int f, void *context);
1.29 paf 156:
157: /**
1.71 paf 158: lock specified file exclusively,
159: do actions under lock.
160: throws an exception in case of problems
1.72 paf 161:
162: if block=false does non-blocking lock
163: @returns true if locked OK, or false if non-blocking locking failed
1.71 paf 164: */
1.72 paf 165: bool file_write_action_under_lock(
1.71 paf 166: const String& file_spec,
1.114 misha 167: const char* action_name,
168: File_write_action action,
169: void *context,
1.71 paf 170: bool as_text=false,
1.72 paf 171: bool do_append=false,
1.77 paf 172: bool do_block=true,
173: bool fail_on_lock_problem=true);
1.71 paf 174:
175: /**
176: write data to specified file,
1.17 paf 177: throws an exception in case of problems
178: */
1.70 paf 179: void file_write(
1.137 misha 180: Request_charsets& charsets,
1.25 paf 181: const String& file_spec,
1.137 misha 182: const char* data,
183: size_t size,
1.58 paf 184: bool as_text,
1.137 misha 185: bool do_append=false,
186: Charset* asked_charset=0);
1.26 paf 187:
188: /**
189: delete specified file
190: throws an exception in case of problems
191: */
1.149 misha 192: bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
1.47 parser 193: /**
194: move specified file
195: throws an exception in case of problems
196: */
1.149 misha 197: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
1.27 paf 198:
1.93 paf 199: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82 paf 200: bool entry_exists(const String& file_spec);
1.107 paf 201: bool file_exist(const String& file_spec);
202: bool dir_exists(const String& file_spec);
203: const String* file_exist(const String& path, const String& name);
1.36 paf 204: bool file_executable(const String& file_spec);
1.37 paf 205:
1.48 parser 206: bool file_stat(const String& file_spec,
1.126 misha 207: size_t& rsize,
208: time_t& ratime,
209: time_t& rmtime,
210: time_t& rctime,
211: bool fail_on_read_problem=true);
1.15 paf 212:
1.154 moko 213: size_t stdout_write(const void *buf, size_t size);
214:
215: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
216:
217: int file_block_read(const int f, unsigned char* buffer, const size_t size);
218:
219: /**
220: String related functions
221: */
222:
223: const char* capitalize(const char* s);
224:
225: /** under WIN32 "t" mode fixes DOS chars OK,
226: can't say that about other systems/ line break styles
1.15 paf 227: */
1.154 moko 228: void fix_line_breaks(char *str, size_t& length /* < may change! used to speedup next actions */);
229:
1.7 paf 230: char *getrow(char **row_ref,char delim='\n');
1.76 paf 231: char *lsplit(char *string, char delim);
1.7 paf 232: char *lsplit(char **string_ref,char delim);
1.8 paf 233: char *rsplit(char *string, char delim);
1.151 moko 234: const char* format(double value, const char *fmt);
1.10 paf 235:
1.142 misha 236: 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 237:
1.148 misha 238: char *search_stop(char*& current, char cstop_at);
239:
1.157 moko 240: inline int pa_strncasecmp(const char* str, const char* substr, size_t count=0) {
241: return strncasecmp(str, substr, count ? count : strlen(substr));
242: }
243:
1.23 paf 244: #ifdef WIN32
245: void back_slashes_to_slashes(char *s);
246: #endif
247:
1.121 misha 248: size_t strpos(const char *str, const char *substr);
249:
1.116 misha 250: int remove_crlf(char *start, char *end);
1.90 paf 251:
1.144 misha 252: inline bool pa_isalpha(unsigned char c) {
253: return (((c>='A') && (c<='Z')) || ((c>='a') && (c<='z')));
254: }
255:
256: inline bool pa_isalnum(unsigned char c) {
257: return (((c>='0') && (c<='9')) || pa_isalpha(c));
258: }
259:
1.154 moko 260: const char* hex_string(unsigned char* bytes, size_t size, bool upcase);
1.155 moko 261: extern const char* hex_digits;
1.99 paf 262:
1.145 misha 263: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size, bool strict=false);
1.104 paf 264: char* pa_base64_encode(const char *in, size_t in_size);
1.112 misha 265: char* pa_base64_encode(const String& file_spec);
1.108 misha 266:
1.158 moko 267: const char *pa_idna_encode(const char *in, Charset &source);
268: const char *pa_idna_decode(const char *in, Charset &source);
269:
1.108 misha 270: const unsigned long pa_crc32(const char *in, size_t in_size);
271: const unsigned long pa_crc32(const String& file_spec);
1.93 paf 272:
1.154 moko 273: /**
274: Mix functions
275: */
1.106 paf 276:
1.164 ! moko 277: #define PA_DEFAULT(A,B) ((A) ? (A):(B) )
! 278:
1.115 misha 279: // some stuff for use with .for_each
1.162 moko 280: static void copy_all_overwrite_to(HashStringValue::key_type key, HashStringValue::value_type value, HashStringValue* dest) {
1.115 misha 281: dest->put(key, value);
282: }
283:
1.162 moko 284: static void remove_key_from(HashStringValue::key_type key, HashStringValue::value_type /*value*/, HashStringValue* dest) {
1.115 misha 285: dest->remove(key);
286: }
287:
1.154 moko 288: Charset* detect_charset(const char* content_type);
289:
1.93 paf 290: // globals
291:
292: extern const String file_status_name;
1.1 paf 293:
1.103 paf 294: // global defines for file options which are handled but not checked elsewhere, we check them
295:
296: #define PA_SQL_LIMIT_NAME "limit"
297: #define PA_SQL_OFFSET_NAME "offset"
298: #define PA_COLUMN_SEPARATOR_NAME "separator"
299: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113 misha 300: #define PA_CHARSET_NAME "charset"
1.150 moko 301: #define PA_RESPONSE_CHARSET_NAME "response-charset"
1.103 paf 302:
1.115 misha 303: // globals defines for sql options
304:
305: #define SQL_BIND_NAME "bind"
306: #define SQL_DEFAULT_NAME "default"
307: #define SQL_DISTINCT_NAME "distinct"
308: #define SQL_VALUE_TYPE_NAME "type"
309:
310: #ifndef DOXYGEN
311: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
312: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE };
313: #endif
314:
1.1 paf 315: #endif
1.115 misha 316:
E-mail: