[HTTP/C++]请求体form-data解析

/ 489评论 / 3910阅读 / 10点赞

预备知识

URL编解码

请求体编码格式


form-data格式

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="myfile"; filename="hello.gif" filename*=UTF-8''hello.gif
Content-Type: image/gif


{二进制数据}
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="mytext"

coolight
------WebKitFormBoundary7MA4YWxkTrZu0gW--
--{boundary}(\r\n)
Content-Disposition: form-data; name="{key}"(\r\n)
(\r\n)
{value}(\r\n)
--{boundary}
--{boundary}(\r\n)
Content-Disposition: form-data; name="{key}"; filename="{filename}"(\r\n)[; filename*={编码方式}''{对应编码的filename}]
Content-Type: {文件格式}(\r\n)
(\r\n)
(\r\n)
{二进制数据}(\r\n)
--{boundary}

c++解析

这里使用正则表达式标准库匹配,并使用string_view减小开销。

#include <regex>
#include <string_view>
#include <map>

using std::string;
using std::multimap;
using std::string_view;

namespace mimicry {
	struct Http_form_data_s {
		bool isFile = false;	//是否为文件,否的话fileName和fileType是无效的
		//注意:fileName可能会包含./ ../等路径信息,此时可能会有一些危险的操作,建议截取最后一个/之后的文件名或自行命名
		std::string fileName;
		std::string fileName_CharEncoding;
		//有时可能文件名会乱码,因此请求中会有指定编码的文件名,对应字段 fileName*
		std::string fileName_;
		std::string fileType;
		std::string value;
	};

std::multimap<std::string, mimicry::Http_form_data_s>
read_form_data(const std::string& in_bound, const std::string& in_data) {
	std::multimap<std::string, mimicry::Http_form_data_s> remap;
	string_view view{ in_data };
	std::regex reg_name{"\\r\\nContent-Disposition:\\s*form-data;\\s*name=\"(.*)\"(?:\\s*;\\s*filename=\"(.*)\"(?:\\s*;\\s*filename\\*=(.*)''(.*))?\\r\\nContent-Type:\\s*(.*))?\\r\\n\\r\\n"};
	string find_bound = "--" + in_bound;
	mimicry::Http_form_data_s data;
	size_t pos = 0, pos_2 = 0;
	if ((pos = view.find(find_bound)) != string::npos) {
		pos += find_bound.size();
		for (;(pos_2 = view.find(find_bound, pos)) != string::npos;) {
			string_view block = view.substr(pos, pos_2 - pos);	//截取两个bound之间的内容
			std::match_results<string_view::const_iterator> block_match_view;
			if (std::regex_search(block.begin(), block.end(), block_match_view, reg_name)) {	//匹配 name 和可能存在的 filename
				data.fileName = block_match_view.str(2);
				data.fileName_CharEncoding = block_match_view.str(3);
				data.fileName_ = block_match_view.str(4);
				data.fileType = block_match_view.str(5);
				data.isFile = data.fileName.size() > 0;
				const char* str_p = &(* (block_match_view[0].second));
				size_t len = block.end() - 2 - block_match_view[0].second;
				//const string_view var_view = string_view{ block_match_view[0].second, block.end() - 2 };
				data.value = string(str_p, len);
				remap.insert({ block_match_view.str(1), data });
			}
			pos = pos_2 + find_bound.size();
		}
	}
	return remap;
}
};

其他

boundary的来历

form-data和Birnary的选择

  1. Jamesexite说道:

    Buy Cialis online: Cialis 20mg price in USA – cheapest cialis

  2. Georgefrege说道:

    buy Kamagra: kamagra pills – п»їkamagra

  3. Charlesvox说道:

    http://kamagra.win/# sildenafil oral jelly 100mg kamagra

  4. Elmersoalf说道:

    Kamagra tablets kamagra pills п»їkamagra

  5. Tylerreunk说道:

    https://kamagra.win/# buy kamagra online usa

  6. DavidBiolo说道:

    https://viagras.online/# Sildenafil Citrate Tablets 100mg

  7. Elmersoalf说道:

    п»їBuy generic 100mg Viagra online Buy Viagra online Cheap generic Viagra online

  8. Charlesvox说道:

    https://cenforce.pro/# Purchase Cenforce Online

  9. Jamesexite说道:

    Generic Levitra 20mg: Vardenafil price – Levitra 10 mg buy online

  10. Elmersoalf说道:

    Cenforce 100mg tablets for sale cenforce.pro Purchase Cenforce Online

  11. Georgefrege说道:

    Cheap Cialis: cialist.pro – Buy Tadalafil 20mg

  12. Charlesvox说道:

    http://cialist.pro/# Buy Cialis online

  13. Jamesexite说道:

    Vardenafil online prescription: Levitra 20mg price – Generic Levitra 20mg

  14. Elmersoalf说道:

    buy kamagra online usa buy kamagra online Kamagra 100mg price

  15. RonaldSwobe说道:

    https://cytotec.club/# Cytotec 200mcg price

  16. WilliamRen说道:

    http://nolvadex.life/# arimidex vs tamoxifen bodybuilding

  17. RandallGuesE说道:

    lisinopril 20mg: lisinopril 20 mg tab price – lisinopril 120mg

  18. Davidfrify说道:

    Abortion pills online buy cytotec buy cytotec in usa

  19. WilliamRen说道:

    http://cytotec.club/# п»їcytotec pills online

  20. RandallGuesE说道:

    buy generic ciprofloxacin: ciprofloxacin order online – buy generic ciprofloxacin

  21. Davidfrify说道:

    buy misoprostol over the counter Cytotec 200mcg price buy cytotec in usa

  22. RonaldSwobe说道:

    http://nolvadex.life/# does tamoxifen make you tired

  23. WilliamRen说道:

    https://nolvadex.life/# tamoxifen dose

  24. Davidfrify说道:

    where to buy lisinopril 2.5 mg lisinopril sale lisinopril price 10 mg

  25. RandallGuesE说道:

    cytotec pills online: buy cytotec pills – buy cytotec over the counter

  26. WilliamRen说道:

    https://nolvadex.life/# nolvadex only pct

  27. Davidfrify说道:

    ciprofloxacin 500mg buy online buy cipro online canada cipro ciprofloxacin

  28. RandallGuesE说道:

    cipro for sale: buy cipro online without prescription – buy ciprofloxacin over the counter

  29. WilliamRen说道:

    http://finasteride.store/# cost generic propecia tablets

  30. RonaldSwobe说道:

    https://finasteride.store/# cost generic propecia prices

  31. RandallGuesE说道:

    propecia no prescription: cheap propecia without insurance – cost cheap propecia for sale

  32. Davidfrify说道:

    lisinopril 20 mg pill lisinopril 20 mg canadian pharmacy zestril 2.5

  33. WilliamRen说道:

    https://finasteride.store/# cost cheap propecia online

  34. RandallGuesE说道:

    cipro for sale: cipro for sale – buy cipro online without prescription

  35. criminal说道:

    I strongly recommend to avoid this platform. The experience I had with it has been nothing but frustration as well as doubts about deceptive behavior. Be extremely cautious, or better yet, look for an honest platform for your needs.I highly advise to avoid this site. My personal experience with it has been purely disappointment as well as doubts about scamming practices. Be extremely cautious, or better yet, find a more reputable service for your needs.

  36. Davidfrify说道:

    how much is lisinopril 5 mg lisinopril 40 mg purchase prinivil cost

  37. lose money说道:

    I highly advise steer clear of this platform. The experience I had with it was nothing but frustration as well as concerns regarding deceptive behavior. Proceed with extreme caution, or even better, find a trustworthy service to fulfill your requirements.I urge you to avoid this site. The experience I had with it has been purely frustration and doubts about fraudulent activities. Proceed with extreme caution, or better yet, find an honest platform to fulfill your requirements.

  38. WilliamRen说道:

    http://cytotec.club/# cytotec buy online usa

  39. Davidfrify说道:

    zestoretic price lisinopril cost uk lisinopril in usa

  40. RonaldSwobe说道:

    https://cytotec.club/# cytotec abortion pill

  41. WilliamRen说道:

    http://finasteride.store/# get propecia no prescription

  42. RandallGuesE说道:

    cytotec pills buy online: cytotec pills buy online – Abortion pills online

  43. Davidfrify说道:

    propecia medication cost of propecia without insurance buy propecia for sale

  44. WilliamRen说道:

    https://cytotec.club/# purchase cytotec

  45. Davidfrify说道:

    cheapest lisinopril 10 mg lisinopril 10mg tablet can i buy lisinopril online

  46. RandallGuesE说道:

    buy generic ciprofloxacin: cipro ciprofloxacin – cipro

  47. RonaldSwobe说道:

    https://nolvadex.life/# nolvadex for pct

  48. Davidfrify说道:

    femara vs tamoxifen tamoxifen dosage does tamoxifen cause menopause

  49. WilliamRen说道:

    http://lisinopril.network/# lisinopril 2.5 mg tablet

  50. RandallGuesE说道:

    purchase cytotec: buy cytotec online fast delivery – purchase cytotec

回复 WilliamRen 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注