Annotation of parser3/src/types/pa_vfile.C, revision 1.75
1.34 paf 1:
1.5 paf 2: /** @file
1.14 paf 3: Parser: @b file parser type.
1.3 paf 4:
1.65 moko 5: Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.26 paf 6: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.30 paf 7: */
1.3 paf 8:
1.37 paf 9: #include "classes.h"
1.1 paf 10: #include "pa_vfile.h"
11: #include "pa_vstring.h"
1.3 paf 12: #include "pa_vint.h"
1.70 moko 13: #include "pa_charsets.h"
1.50 misha 14: #include "pa_request.h"
1.1 paf 15:
1.75 ! moko 16: volatile const char * IDENT_PA_VFILE_C="$Id: pa_vfile.C,v 1.74 2016/12/23 15:30:16 moko Exp $" IDENT_PA_VFILE_H;
1.51 moko 17:
1.37 paf 18: // externs
19:
20: extern Methoded* file_class;
21:
22: // defines for statics
23:
24: #define SIZE_NAME "size"
25: #define TEXT_NAME "text"
26:
1.50 misha 27: #define MODE_VALUE_TEXT "text"
28: #define MODE_VALUE_BINARY "binary"
29:
1.58 moko 30: #define CONTENT_TYPE_TEXT "text/plain"
31: #define CONTENT_TYPE_BINARY "application/octet-stream"
32:
1.37 paf 33: // statics
34:
35: static const String size_name(SIZE_NAME);
36: static const String text_name(TEXT_NAME);
1.50 misha 37:
38: static const String mode_value_text(MODE_VALUE_TEXT);
39: static const String mode_value_binary(MODE_VALUE_BINARY);
1.37 paf 40:
1.58 moko 41: static const String content_type_text(CONTENT_TYPE_TEXT);
42: static const String content_type_binary(CONTENT_TYPE_BINARY);
43:
1.59 moko 44: inline bool content_type_is_default(Value *content_type){
45: if(content_type){
46: const String *ct=content_type->get_string();
47: return ct == &content_type_text || ct == &content_type_binary;
48: }
49: return true;
50: }
51:
1.37 paf 52: // methods
53:
54: VStateless_class *VFile::get_class() { return file_class; }
55:
1.75 ! moko 56: HashStringValue *VFile::get_hash() { Value *prefetch __attribute__((unused))=get_element(text_name); return &ffields; }
1.74 moko 57:
58:
1.61 moko 59: void VFile::set_all(bool atainted, bool ais_text_mode, const char* avalue_ptr, size_t avalue_size, const String* afile_name) {
1.8 paf 60: fvalue_ptr=avalue_ptr;
61: fvalue_size=avalue_size;
1.53 misha 62:
1.43 misha 63: ftext_tainted=atainted;
1.60 moko 64: fis_text_content=ais_text_mode;
1.8 paf 65:
1.10 paf 66: ffields.clear();
1.43 misha 67:
1.60 moko 68: set_name(afile_name);
1.73 moko 69: ffields.put(size_name, new VDouble(fvalue_size));
1.58 moko 70: set_mode(ais_text_mode);
1.54 moko 71: }
1.53 misha 72:
1.54 moko 73: void VFile::set(bool atainted, bool ais_text_mode, char* avalue_ptr, size_t avalue_size, const String* afile_name, Value* acontent_type, Request* r) {
74: if(ais_text_mode && avalue_ptr && avalue_size) {
75: fix_line_breaks(avalue_ptr, avalue_size);
76: }
1.61 moko 77: set_all(atainted, ais_text_mode, avalue_ptr, avalue_size, afile_name);
1.60 moko 78: set_content_type(acontent_type, afile_name, r);
1.50 misha 79: }
80:
1.54 moko 81: void VFile::set_binary(bool atainted, const char* avalue_ptr, size_t avalue_size, const String* afile_name, Value* acontent_type, Request* r) {
1.61 moko 82: set_all(atainted, false, avalue_ptr, avalue_size, afile_name);
1.60 moko 83: set_content_type(acontent_type, afile_name, r);
84: }
85:
86: void VFile::set_binary_string(bool atainted, const char* avalue_ptr, size_t avalue_size) {
1.61 moko 87: set_all(atainted, false, avalue_ptr, avalue_size, 0);
1.54 moko 88: }
1.53 misha 89:
1.71 moko 90: void VFile::set(VFile& avfile, bool *ais_text_mode, const String* afile_name, Value* acontent_type, Request* r) {
1.58 moko 91: fvalue_ptr=avfile.fvalue_ptr;
92: fvalue_size=avfile.fvalue_size;
93: ftext_tainted=avfile.ftext_tainted;
1.69 moko 94: fis_text_mode=avfile.fis_text_mode;
1.58 moko 95: fis_text_content=avfile.fis_text_content;
1.53 misha 96:
97: ffields.clear();
1.50 misha 98: for(HashStringValue::Iterator i(avfile.ffields); i; i.next())
1.53 misha 99: if(i.key() != text_name) // do not copy cached .text value
1.58 moko 100: ffields.put(*new String(i.key(), String::L_TAINTED), i.value());
1.54 moko 101:
1.71 moko 102: if(ais_text_mode)
103: set_mode(*ais_text_mode);
1.58 moko 104:
105: if(afile_name)
106: set_name(afile_name);
107:
1.71 moko 108: if(acontent_type || afile_name || ( ais_text_mode && content_type_is_default(ffields.get(content_type_name)) ))
1.58 moko 109: set_content_type(acontent_type, afile_name, r);
1.53 misha 110: }
111:
112: const char* VFile::text_cstr() {
113: const char* p=value_ptr();
1.54 moko 114: if(fis_text_content)
1.53 misha 115: return p;
116:
117: size_t size=fvalue_size;
118:
119: if(const char *premature_zero_pos=(const char *)memchr(p, 0, size))
120: size=premature_zero_pos-p;
121:
1.63 moko 122: char *copy_ptr=size?pa_strdup(p, size):0;
1.54 moko 123: // text mode but binary content
1.53 misha 124: if(fis_text_mode && size)
125: fix_line_breaks(copy_ptr, size);
126: return copy_ptr;
1.50 misha 127: }
128:
1.53 misha 129: void VFile::set_mode(bool ais_text_mode){
130: fis_text_mode=ais_text_mode;
1.56 moko 131: if(fvalue_ptr)
132: ffields.put(mode_name, new VString(ais_text_mode? mode_value_text : mode_value_binary ));
1.50 misha 133: }
134:
135: void VFile::set_name(const String* afile_name){
1.14 paf 136: char *lfile_name;
1.57 misha 137: if(afile_name && !afile_name->is_empty()) {
1.67 moko 138: if(afile_name->starts_with("http://") || afile_name->starts_with("https://")){
139: size_t query=afile_name->pos('?');
140: if(query!=STRING_NOT_FOUND)
141: afile_name=&afile_name->mid(0,query);
142: }
1.63 moko 143: lfile_name=pa_strdup(afile_name->taint_cstr(String::L_FILE_SPEC));
1.10 paf 144: if(char *after_slash=rsplit(lfile_name, '\\'))
145: lfile_name=after_slash;
146: if(char *after_slash=rsplit(lfile_name, '/'))
147: lfile_name=after_slash;
1.66 moko 148: if(!lfile_name[0])
149: lfile_name=(char *)NONAME_DAT;
1.14 paf 150: } else
1.54 moko 151: lfile_name=(char *)NONAME_DAT;
1.50 misha 152:
1.53 misha 153: ffields.put(name_name, new VString(*new String(lfile_name, String::L_FILE_SPEC)));
1.50 misha 154: }
1.43 misha 155:
1.50 misha 156: void VFile::set_content_type(Value* acontent_type, const String* afile_name, Request* r){
157: if(!acontent_type && afile_name && r)
158: acontent_type=new VString(r->mime_type_of(afile_name));
1.43 misha 159:
1.58 moko 160: if(!acontent_type)
161: acontent_type=new VString(fis_text_mode ? content_type_text : content_type_binary);
162:
163: ffields.put(content_type_name, acontent_type);
1.43 misha 164: }
165:
1.70 moko 166: Charset* VFile::detect_binary_charset(Charset *charset){
167: if(!charset)
1.68 moko 168: if(Value* content_type=ffields.get(content_type_name))
169: if(const String *ct=content_type->get_string())
1.70 moko 170: charset=detect_charset(ct->cstr());
1.72 moko 171: return pa_charsets.checkBOM((char*&)fvalue_ptr, fvalue_size, charset); // checkBOM can alter ptr, but not the content
1.68 moko 172: }
173:
174: void VFile::transcode(Charset& from_charset, Charset& to_charset){
175: String::C result=Charset::transcode(String::C(fvalue_ptr, fvalue_size), from_charset, to_charset);
176: fvalue_ptr=result.str;
177: fvalue_size=result.length;
178: ffields.put(size_name, new VInt(fvalue_size));
179: }
180:
1.46 misha 181: void VFile::save(Request_charsets& charsets, const String& file_spec, bool is_text, Charset* asked_charset) {
182: if(fvalue_ptr)
183: file_write(charsets, file_spec, fvalue_ptr, fvalue_size, is_text, false/*do_append*/, asked_charset);
184: else
1.58 moko 185: throw Exception(PARSER_RUNTIME, &file_spec, "saving stat-ed file");
1.46 misha 186: }
187:
1.50 misha 188: bool VFile::is_text_mode(const String& mode) {
189: if(mode==mode_value_text)
190: return true;
191: if(mode==mode_value_binary)
192: return false;
1.62 moko 193: throw Exception(PARSER_RUNTIME, &mode, "is invalid mode, must be either '" MODE_VALUE_TEXT "' or '" MODE_VALUE_BINARY "'");
1.50 misha 194: }
1.58 moko 195:
1.68 moko 196: bool VFile::is_valid_mode(const String& mode) {
1.50 misha 197: return (mode==mode_value_text || mode==mode_value_binary);
198: }
199:
1.45 misha 200: Value* VFile::get_element(const String& aname) {
1.43 misha 201: Value* result;
202:
203: // $method
1.45 misha 204: if(result=VStateless_object::get_element(aname))
1.43 misha 205: return result;
206:
207: // $field
208: if(result=ffields.get(aname))
209: return result;
210:
211: // $text - if not cached
1.53 misha 212: if(aname == text_name && fvalue_ptr && fvalue_size) {
1.43 misha 213: // assigned file have ptr and we really have some bytes
1.42 misha 214:
1.53 misha 215: result=new VString(*new String(text_cstr(), ftext_tainted ? String::L_TAINTED : String::L_AS_IS));
1.42 misha 216:
1.53 misha 217: // cache it
1.43 misha 218: ffields.put(text_name, result);
1.35 paf 219:
220: return result;
1.43 misha 221: }
1.35 paf 222:
1.43 misha 223: return 0;
1.3 paf 224: }
1.48 misha 225:
1.52 moko 226: const String* VFile::get_json_string(Json_options& options){
1.49 moko 227: String& result=*new String("{\n", String::L_AS_IS);
228:
229: String * indent=NULL;
230:
1.52 moko 231: if (options.indent){
232: indent = new String(",\n\t", String::L_AS_IS); *indent << options.indent << "\"";
233: result << "\t" << options.indent;
1.49 moko 234: }
235:
236: result << "\"class\":\"file\"";
1.48 misha 237:
238: for(HashStringValue::Iterator i(ffields); i; i.next() ){
239: String::Body key=i.key();
240: if(key != text_name){
1.49 moko 241: indent ? result << *indent : result << ",\n\"";
242: result << String(key, String::L_JSON) << "\":" << *i.value()->get_json_string(options);
1.48 misha 243: }
244: }
245:
246: if(fvalue_ptr){
1.52 moko 247: switch(options.file){
1.48 misha 248: case Json_options::F_BASE64:
249: {
1.49 moko 250: indent ? result << *indent : result << ",\n\"";
251: result << "base64\":\"";
1.48 misha 252: const char* encoded=pa_base64_encode(fvalue_ptr, fvalue_size);
1.55 moko 253: result.append_help_length(encoded, 0, String::L_JSON);
1.49 moko 254: result << "\"";
1.48 misha 255: break;
256: }
257: case Json_options::F_TEXT:
258: {
1.49 moko 259: indent ? result << *indent : result << ",\n\"";
260: result << "text\":\"";
1.55 moko 261: result.append_help_length(text_cstr(), 0, String::L_JSON);
1.49 moko 262: result << "\"";
1.48 misha 263: break;
264: }
1.64 moko 265: case Json_options::F_BODYLESS: break;
1.48 misha 266: }
267: }
268:
1.52 moko 269: result << "\n" << options.indent << "}";
1.49 moko 270: return &result;
1.48 misha 271: }
E-mail: