这段代码应该解析请求,提取URI路径,然后提取每个单独的头及其值,然后将所有提取的变量传递给std::vector <const char*> cva
,最终将传递给std::vector<std::vector<const char*>> nv (config.nv.push_back(std::move(cva));)
< / p>
我在这里遇到的问题很少:
print
语句,表明我的解析已经正确完成。 for
循环(在结尾处)具有与预期不同的输出。在这里我经历了另一个问题cva.push_back(nullptr)
,如果我保留这个语句,for
循环打印第一个元素然后退出,但是当我删除该语句时,它工作正常。为什么?我很感激任何帮助,尤其是我希望了解更多关于C ++的知识,但似乎我在一个太高级的问题上遇到了磕磕绊绊
// I already have a reqlines vector<std::string> variable holding my requests
// each request is formed as follows
// http://<hostname>:<portnumber>/path<TAB>header1:value1<TAB>header2:value2<TAB>header3:tab3<TAB>header4:tab4<etc.....>
// Pretty much a TAB separated string with request at beginning followed by extra headers
std::vector<std::string> paths;
std::vector<std::string> extraheaders;
std::vector<std::vector<std::string>> tokenized;
int count = 0; // keeps track of each request number so I can access its corresponding extra headers from extraheaders vector
if (condition is met){
// creating which will be used to store my requests paths as well as extra headers
// This has to be a const char * vector since it will be used by an external library which requires such type
std::vector<const char *> cva;
for (auto &req : reqlines){
unsigned int pos = req.find_first_of("\t", 0);
if (pos == -1){
paths.push_back(req);
extraheaders.push_back(" ");
} else {
paths.push_back(req.substr(0, pos));
extraheaders.push_back(req.substr(pos+1, std::string::npos));
}
}
for (auto &path : paths){
cva.push_back(":path");
cva.push_back(path.c_str()); // adding the URI path into cva variable
// explode function which returns a std::vector<std::string> when passing an std::string to it
tokenized.push_back(explode(extraheaders[count], ' ')); // extracting the vector<std::string> of all extra headers
// if (tokenized[count][0].compare(" ") == 0){
// printf(" %d element is empty is skipped \n");
// }else {
for (auto &tok : tokenized[count]){ // looping through extra headers of request number "count", parsing header name/value and adding it to cva
printf(" %d tok %s\n", __LINE__, tok.c_str());
printf(" %d tok address %d\n", __LINE__, &tok);
unsigned int pos = tok.find_first_of(":", 0);
if (pos == -1 )
printf(" %d there are no headers \n", __LINE__);
else {
printf("header name: %s\n", (tok.substr(0, pos)).c_str());
printf("header value: %s\n", (tok.substr(pos+1, std::string::npos)).c_str());
cva.push_back((tok.substr(0, pos)).c_str());
cva.push_back((tok.substr(pos+1, std::string::npos)).c_str());
}
}
cva.push_back(":version"); // adding version header
cva.push_back("HTTP/1.1"); // adding version header number
cva.push_back(nullptr); // adding nullptr (which is how nv is expecting cva to be terminated)
count++;
// passing the cva content to nv
config.nv.push_back(std::move(cva));
}
// Below are the printing statement to check the values of the different variables I created and populated
// above, my problem is that the population process puts some values into my variables however printing
// the content of the those variables shows different values from what I am expecting
std::cout << " " << std::endl << std::endl;
std::cout << "Printing cva" << std::endl;
for (auto &elem : cva){
std::cout << &elem << " " <<elem << std::endl;
}
std::cout << " " << std::endl << std::endl;
std::cout << "Printing paths" << std::endl;
for (auto &path : paths){
std::cout << &path << " " << path << std::endl;
}
std::cout << " " << std::endl << std::endl;
std::cout << "Printing headers" << std::endl;
for (auto &hed : extraheaders) {
std::cout << &hed << " "<<hed << std::endl;
}
}
<小时/> 假设我有一个看起来像这样的文件:
https://<endpoint>:<port>/path1
https://<endpoint>:<port>/path2<TAB>PCC:1234<TAB>Cookie:dsgfshg
https://<endpoint>:<port>/path3<TAB>PCC:5678
https://<endpoint>:<port>/path4
https://<endpoint>:<port>/path5
预期输出应为:
Printing cva
:path
path1
:path
path2
:PCC
1234
:Cookie
dsgfshg
:path
path3
:PCC
5678
:path
path4
:path
path5
但实际输出是:
Printing cva
:path
path1
:path
path2
:PCC
5678 //Different - Wrong value
//Different - Cookie + value is missing
:path
path3
:PCC
5678
:path
path4
:path
path5
注意:打印headers
和path
缺失,因为它们是相同的