MCPcopy Create free account
hub / github.com/SmingHub/Sming / partHeadersComplete

Method partHeadersComplete

Sming/Libraries/MultipartParser/src/MultipartParser.cpp:115–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

113}
114
115int MultipartParser::partHeadersComplete(multipart_parser_t* p)
116{
117 GET_PARSER();
118
119 auto& headers = static_cast<const HttpHeaders&>(parser->incomingHeaders);
120 String headerValue = headers[HTTP_HEADER_CONTENT_DISPOSITION];
121 if(!headerValue) {
122 return 0;
123 }
124
125 // Content-Disposition: form-data; name="image"; filename=".gitignore"
126 // Content-Disposition: form-data; name="data"
127 int startPos = headerValue.indexOf(F("name="));
128 if(startPos < 0) {
129 debug_e("Invalid header content");
130 return -1; // Invalid header content
131 }
132 startPos += 6; // name="
133 int endPos = headerValue.indexOf(';', startPos);
134
135 String name;
136 if(endPos < 0) {
137 name = headerValue.substring(startPos, headerValue.length() - 1);
138 } else {
139 name = headerValue.substring(startPos, endPos - 1);
140 }
141 // get stream corresponding to field name
142 parser->stream = parser->request.files[name];
143
144 // inject file name, if any
145 startPos = headerValue.indexOf(F("filename="));
146 if(startPos < 0) {
147 return 0;
148 }
149
150 startPos += 10; // filename="
151 endPos = headerValue.indexOf('"', startPos);
152 if(endPos < 0) {
153 return 0;
154 }
155
156 String fileName = headerValue.substring(startPos, endPos);
157 // sanitize the name -> remove any slashes and trailing dots
158 fileName.replace('/', '-');
159 fileName.trim(".");
160 if(fileName.length() == 0) {
161 return 0;
162 }
163
164 // if the stream is of type FileStream and the name is not set
165 // then we can set the name and flags to create-write
166 auto stream = parser->stream;
167 if(stream == nullptr) {
168 return 0;
169 }
170
171 if(stream->getStreamType() == eSST_Wrapper) {
172 auto wrapper = static_cast<StreamWrapper*>(stream);

Callers

nothing calls this directly

Calls 12

getSourceMethod · 0.80
fileNameMethod · 0.80
FFunction · 0.50
indexOfMethod · 0.45
substringMethod · 0.45
lengthMethod · 0.45
replaceMethod · 0.45
trimMethod · 0.45
getStreamTypeMethod · 0.45
openMethod · 0.45
toIntMethod · 0.45
checkHeadersMethod · 0.45

Tested by

no test coverage detected