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

/ 338评论 / 2653阅读 / 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. lose money说道:

    I urge you to avoid this platform. The experience I had with it was only dismay as well as suspicion of deceptive behavior. Proceed with extreme caution, or alternatively, find a more reputable site for your needs.

  2. lose money说道:

    I urge you stay away from this platform. My own encounter with it was purely dismay along with concerns regarding fraudulent activities. Proceed with extreme caution, or better yet, find an honest site for your needs.

  3. lose money说道:

    I urge you stay away from this platform. My own encounter with it has been purely frustration as well as doubts about deceptive behavior. Proceed with extreme caution, or better yet, find an honest platform to meet your needs.

  4. lose money说道:

    I strongly recommend to avoid this site. My personal experience with it was nothing but disappointment and suspicion of fraudulent activities. Be extremely cautious, or even better, find a more reputable platform to fulfill your requirements.

  5. lose money说道:

    I strongly recommend stay away from this platform. My personal experience with it was only dismay along with doubts about deceptive behavior. Be extremely cautious, or better yet, seek out a trustworthy site to fulfill your requirements.

  6. lose money说道:

    I strongly recommend steer clear of this platform. The experience I had with it has been only disappointment along with suspicion of deceptive behavior. Proceed with extreme caution, or alternatively, seek out a more reputable platform for your needs.

  7. lose money说道:

    I urge you to avoid this site. The experience I had with it has been purely dismay along with doubts about fraudulent activities. Exercise extreme caution, or even better, seek out a more reputable platform to meet your needs.

  8. lose money说道:

    I highly advise steer clear of this site. My personal experience with it has been only frustration and suspicion of fraudulent activities. Be extremely cautious, or alternatively, seek out a more reputable platform to meet your needs.

  9. criminal说道:

    I strongly recommend to avoid this platform. My own encounter with it was purely dismay as well as concerns regarding fraudulent activities. Exercise extreme caution, or alternatively, find an honest platform to fulfill your requirements.

  10. lose money说道:

    I strongly recommend steer clear of this site. My own encounter with it was nothing but dismay and concerns regarding scamming practices. Proceed with extreme caution, or even better, find an honest service to fulfill your requirements.

  11. lose money说道:

    I strongly recommend stay away from this platform. My own encounter with it has been purely frustration and suspicion of deceptive behavior. Exercise extreme caution, or even better, find an honest service for your needs.

  12. criminal说道:

    I highly advise stay away from this platform. My own encounter with it has been only frustration along with doubts about deceptive behavior. Exercise extreme caution, or even better, find a trustworthy service for your needs.

  13. criminal说道:

    I strongly recommend steer clear of this platform. My personal experience with it has been only frustration and doubts about fraudulent activities. Proceed with extreme caution, or better yet, find an honest site to meet your needs.

  14. scam说道:

    I strongly recommend steer clear of this site. My personal experience with it was only disappointment along with concerns regarding scamming practices. Exercise extreme caution, or alternatively, seek out a trustworthy service to fulfill your requirements.

  15. scam说道:

    I highly advise to avoid this platform. My own encounter with it has been only frustration as well as suspicion of scamming practices. Proceed with extreme caution, or better yet, seek out a trustworthy service for your needs.

  16. criminal说道:

    I urge you stay away from this site. My personal experience with it was nothing but dismay and doubts about scamming practices. Be extremely cautious, or alternatively, look for an honest site to meet your needs.

  17. lose money说道:

    I highly advise steer clear of this platform. The experience I had with it has been only dismay along with concerns regarding fraudulent activities. Proceed with extreme caution, or better yet, look for an honest platform to fulfill your requirements.

  18. criminal说道:

    I highly advise to avoid this platform. My personal experience with it was only dismay and concerns regarding fraudulent activities. Exercise extreme caution, or better yet, seek out an honest platform for your needs.

  19. criminal说道:

    I urge you steer clear of this platform. My own encounter with it was nothing but dismay and concerns regarding deceptive behavior. Proceed with extreme caution, or alternatively, look for a more reputable platform to meet your needs.

  20. lose money说道:

    Amazing! I just finished reading your blog post and I’m thoroughly impressed. Your insight on this topic is extremely valuable. I’ve gained a new perspective and am eager to read more. Keep up the great work!

  21. lose money说道:

    Amazing! I recently read your blog post and I’m thoroughly impressed. Your insight on the topic is extremely valuable. I’ve gained a new perspective and am eager to read more. Thanks for sharing!

  22. lose money说道:

    Incredible! I just read your post and I’m blown away. Your perspective on the topic is incredibly insightful. It really made me think and can’t wait to read more. Your work is inspiring!

  23. lose money说道:

    Amazing! I recently read your article and I’m thoroughly impressed. Your insight on the topic is incredibly insightful. I’ve learned so much and am eager to see your next post. Keep up the great work!

  24. lose money说道:

    Fantastic! I recently read your blog post and I’m absolutely amazed. Your perspective on the topic is extremely valuable. It really made me think and am eager to read more. Your work is inspiring!

  25. criminal说道:

    Incredible! I recently read your post and I’m blown away. Your perspective on this subject is extremely valuable. I’ve learned so much and can’t wait to see what you write next. Thanks for sharing!

  26. scam说道:

    Fantastic! I just read your post and I’m blown away. Your insight on this subject is spot-on. It really made me think and can’t wait to read more. Thanks for sharing!

  27. lose money说道:

    Amazing! I recently read your blog post and I’m absolutely amazed. Your perspective on this topic is incredibly insightful. I’ve gained a new perspective and can’t wait to see your next post. Your work is inspiring!

  28. criminal说道:

    Wow! I just finished reading your blog post and I’m blown away. Your analysis on this subject is spot-on. I’ve learned so much and can’t wait to see what you write next. Your work is inspiring!

  29. lose money说道:

    Fantastic! I just read your post and I’m blown away. Your perspective on this subject is incredibly insightful. I’ve gained a new perspective and can’t wait to see your next post. Keep up the great work!

发表回复

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