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