2011-08-06 1 views
0

Поэтому я стараюсь такой код:Как получить имя файла из строки запроса HTTP-запроса?

std::ofstream myfile; 
myfile.open ("example.txt", std::ios_base::app); 
myfile << "Request body: " << request->body << std::endl << "Request size: " << request->body.length() << std::endl; 

size_t found_file = request->body.find("filename="); 
if (found_file != std::string::npos) 
{ 
    size_t end_of_file_name = request->body.find("\"",found_file + 1); 
    if (end_of_file_name != std::string::npos) 
    { 
     std::string filename(request->body, found_file+10, end_of_file_name - found_file); 
     myfile << "Filename == " << filename << std::endl; 
    } 
} 
myfile.close(); 

Но он выводит, например, в:

Request body: ------WebKitFormBoundary0tbfYpUAzAlgztXL 

Content-Disposition: form-data; name="datafile"; filename="Torrent downloaded from Demonoid.com.txt" 

Content-Type: text/plain 



Torrent downloaded from http://www.Demonoid.com 

------WebKitFormBoundary0tbfYpUAzAlgztXL-- 


Request size: 265 
Filename == Torrent d 

Это означает, что с filename="Torrent downloaded from Demonoid.com.txt" моя уступка returnes Torrent d в качестве имени файла в то время как он должен вернуть Torrent downloaded from Demonoid.com.txt. Как исправить мой файл upload http request filename parser?

ответ

3

string::find возвращает индекс первого символа в строке поиска. Поэтому он дает вам индекс f в , когда вы его ищите.

В строке

size_t end_of_file_name = request->body.find("\"",found_file + 1); 

Вы должны изменить что

size_t end_of_file_name = request->body.find("\"", found_file + 9 + 1); // 9 because that's the length of "filename=" and 1 to start at the character after the " 

Затем измените

std::string filename(request->body, found_file+10, end_of_file_name - found_file); 

Для

std::string filename(request->body, found_file + 10, end_of_file_name - (found_file + 10)); 

Возможно, вы захотите добавить еще одну переменную, чтобы добавить, добавив 10 все время.

 Смежные вопросы

  • Нет связанных вопросов^_^