[HTTP/C++]form-data解析

/ 134评论 / 1794阅读 / 9点赞

预备知识

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. scam说道:

    wow, amazing

  2. porn vvsetohimalxxvc.8EmNvOpi8R13

  3. child porn web说道:

    This is amazing, you’ve done an incredible job this time! Your dedication and effort are evident in every detail of this piece. I couldn’t help but express my appreciation for creating such fantastic work with us. You are exceptionally talented and dedicated. Keep up the incredible work! 🌟👏👍

  4. scam说道:

    Spectacular, keep it up

  5. scam说道:

    Remarkable, excellent

  6. porno izle说道:

    escort eyeconartxx.1KTecI9qTSwz

  7. lose money说道:

    nice content!nice history!! boba 😀

  8. craft porn说道:

    sexax 250tldenemebonusuxx.a28ZpaFXwR1c

  9. craft porn说道:

    bahis siteleri porn bingoxx.C4l9F9VZORhU

  10. escort mobileidn.EielzPSXHu8N

  11. seksi siteler说道:

    sexax footballxx.uohn4DeGzo3R

  12. amciik siteleri说道:

    craft porn pokkerx.tbb3wTQZcfwW

  13. scam说道:

    nice content!nice history!! boba 😀

  14. ped说道:

    British Columbia welcomed sixty women from Britain, mail-order brides recruited by the Columbia Emigration Society, in 1862. Another twenty women from Australia were bound for Victoria but were convinced to stay in San Francisco when their ship docked there. Women Enabled International For daters in 2020 with the Japanese language skills to keep up, Omiai has somewhat rebranded itself in more of a Tinder Instagram friendly fashion, and strictly placed focus on it being a relationship marriage-centric app. If you’re seriously looking to meet a marriage partner, this is probably your best option when it comes to dating apps. One of the most important factors in making a long distance relationship work is making time for one another. Texting each other will most likely be your basic form of communication during the day and since you won’t be able to meet each other often, it will play an important part in your relationship as this will help you get to know each other better.
    https://ekm.matsyafedfreshmeen.com/2021/04/26/chat-to-be-able-to-men-and-women-with-http-startsingleparentdating-co-uk-east-sussex-blackham-very-best-on-line-free-dating-online/
    Also keep in mind that some extremely clingy people are on the apps as well. My advice is catered to all dating apps. As the fastest growing religion in the U.S., today there are significant Muslim communities across the country, represented by almost every major ethnic group. An impressive diversity almost equal to that of its origins. We look at some of the opportunities and challenges for modern Muslim singles both in the U.S. and globally, and why eharmony is your best bet finding real love. Once you’ve decided to fight for love, there should be no disappointments and regrets left online. You didn’t receive a reply back, or someone you were chatting with suddenly disappeared? It is okay! Someone nicer will answer, who will truly appreciate the intelligent and funny conversations with you.

  15. porno说道:

    Online bahis dünyası sürekli olarak gelişmekte ve bahis severler için yeni fırsatlar sunmaktadır.

  16. scam说道:

    I played on this gambling site and earned a considerable pile of cash. However, later on, my mom fell seriously sick, and I required to take out some money from my wallet. Unfortunately, I experienced problems and couldn’t withdraw the funds. Tragically, my mom passed away due to this casino site. I urgently request your help in bringing attention to this issue with the online casino. Please help me in seeking justice, to ensure others do not experience the pain I’m facing today, and stop them from facing similar heartache. 😭😭

  17. scam说道:

    I participated on this gambling site and won a substantial pile of cash. However, afterward, my mother fell gravely ill, and I required to cash out some earnings from my account. Unfortunately, I encountered problems and could not finalize the cashout. Tragically, my mother passed away due to this online casino. I urgently request your support in reporting this issue with the platform. Please assist me to find justice, to ensure others won’t have to experience the pain I’m facing today, and stop them from facing similar misfortune. 😭😭

  18. scam说道:

    I tried my luck on this gambling site and earned a considerable sum of cash. However, later on, my mother fell gravely ill, and I wanted to withdraw some funds from my wallet. Unfortunately, I experienced problems and could not finalize the cashout. Tragically, my mother died due to such online casino. I earnestly request your assistance in addressing this issue with the platform. Please help me to find justice, to ensure others do not experience the anguish I’m facing today, and avert them from facing similar heartache. 😭😭

  19. xxx child说道:

    I tried my luck on this online casino platform and secured a substantial sum of money. However, eventually, my mother fell critically ill, and I required to take out some money from my account. Unfortunately, I encountered difficulties and couldn’t complete the withdrawal. Tragically, my mother died due to the casino site. I earnestly ask for your assistance in addressing this issue with the site. Please aid me to obtain justice, to ensure others won’t experience the pain I’m facing today, and prevent them from undergoing similar misfortune. 😭😭

  20. child porn web说道:

    I tried my luck on this gambling site and secured a significant sum of money. However, eventually, my mother fell gravely ill, and I wanted to withdraw some earnings from my wallet. Unfortunately, I encountered issues and was unable to complete the withdrawal. Tragically, my mom passed away due to such online casino. I urgently ask for your assistance in addressing this situation with the site. Please assist me to find justice, to ensure others won’t have to experience the anguish I’m facing today, and avert them from undergoing similar misfortune. 😭😭

  21. minors porn说道:

    I tried my luck on this online casino platform and secured a substantial amount of cash. However, later on, my mom fell seriously sick, and I required to take out some funds from my wallet. Unfortunately, I experienced issues and was unable to withdraw the funds. Tragically, my mother passed away due to this casino site. I earnestly ask for your support in addressing this issue with the online casino. Please help me to find justice, to ensure others won’t experience the pain I’m facing today, and avert them from undergoing similar tragedy. 😭😭

  22. child porn video说道:

    I played on this casino website and won a significant pile of earnings. However, afterward, my mom fell seriously ill, and I needed to cash out some funds from my wallet. Unfortunately, I encountered issues and was unable to complete the withdrawal. Tragically, my mom passed away due to such online casino. I kindly request your assistance in addressing this concern with the online casino. Please assist me to obtain justice, to ensure others won’t face the anguish I’m facing today, and stop them from facing similar tragedy. 😭😭

  23. loli porn video说道:

    I played on this gambling site and earned a significant pile of money. However, later on, my mom fell critically sick, and I wanted to take out some earnings from my account. Unfortunately, I faced problems and couldn’t finalize the cashout. Tragically, my mom passed away due to the gambling platform. I urgently plead for your support in bringing attention to this issue with the online casino. Please assist me to obtain justice, to ensure others won’t experience the hardship I’m facing today, and prevent them from experiencing similar misfortune. 😭😭

  24. loli hentai说道:

    I played on this casino website and won a substantial pile of earnings. However, eventually, my mom fell critically sick, and I required to take out some funds from my account. Unfortunately, I experienced difficulties and couldn’t complete the withdrawal. Tragically, my mom passed away due to the gambling platform. I urgently request your support in reporting this concern with the site. Please assist me to obtain justice, to ensure others do not face the pain I’m facing today, and prevent them from undergoing similar misfortune. 😭😭

  25. child porn说道:

    I participated on this casino website and won a significant amount of cash. However, afterward, my mother fell seriously ill, and I wanted to withdraw some funds from my wallet. Unfortunately, I faced difficulties and was unable to withdraw the funds. Tragically, my mom passed away due to the gambling platform. I earnestly plead for your support in addressing this concern with the platform. Please assist me in seeking justice, to ensure others won’t have to face the anguish I’m facing today, and prevent them from facing similar tragedy. 😭😭

发表回复

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