add tc_http auto test

This commit is contained in:
ruanshudong 2020-06-09 21:03:58 +08:00
parent b318cd79ea
commit e642785516
7 changed files with 5613 additions and 5006 deletions

View File

@ -0,0 +1,841 @@
//
// Created by jarod on 2020/2/20.
//
#include "util/tc_http.h"
#include "util/tc_common.h"
#include "util/tc_file.h"
#include "gtest/gtest.h"
using namespace tars;
class HttpTest : public testing::Test
{
public:
//添加日志
static void SetUpTestCase()
{
cout<<"SetUpTestCase"<<endl;
}
static void TearDownTestCase()
{
cout<<"TearDownTestCase"<<endl;
}
virtual void SetUp() //TEST跑之前会执行SetUp
{
cout<<"SetUp"<<endl;
}
virtual void TearDown() //TEST跑完之后会执行TearDown
{
cout<<"TearDown"<<endl;
}
};
TEST_F(HttpTest, testCheckRequestURL) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
TC_HttpRequest req;
ASSERT_TRUE(req.decode(s));
ASSERT_TRUE(req.getRequestUrl() == "/a/b");
ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
}
TEST_F(HttpTest, testEncodeString) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
// s += string("a", 1024);
TC_HttpRequest req;
req.decode(s);
int64_t t = TC_Common::now2us();
int count = 100000;
int i = 0;
while(++i<count) {
string s;
s = req.encode();
}
ASSERT_TRUE(req.decode(s));
ASSERT_TRUE(req.getRequestUrl() == "/a/b");
ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
cout << "testEncodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testEncodeVector) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
s += string("a", 1024);
TC_HttpRequest req;
req.decode(s);
int64_t t = TC_Common::now2us();
int count = 100000;
int i = 0;
while(++i<count) {
vector<char> buff;
req.encode(buff);
}
cout << "testEncodeVector::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testEncodeBuffString) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
int64_t t = TC_Common::now2us();
TC_HttpRequest req;
req.decode(s);
TC_NetWorkBuffer buff(NULL);
int count = 100000;
int i = 0;
while(++i<count) {
req.encode(buff);
}
cout << "testEncodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testDecodeString) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
int64_t t = TC_Common::now2us();
int count = 100000;
int i = 0;
while(++i<count) {
TC_HttpRequest req;
req.decode(s);
}
cout << "testDecodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testDecodeBuffString) //此时使用的是TEST_F宏
{
string s = string("HTTP/1.1 200 OK\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
int count = 100000;
int i = 0;
vector<TC_NetWorkBuffer> vbuff;
while(i<count) {
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s);
vbuff.push_back(buff);
++i;
}
int64_t t = TC_Common::now2us();
i = 0;
while(i<count) {
TC_HttpResponse req;
ASSERT_TRUE(req.incrementDecode(vbuff[i]));
++i;
}
cout << "testDecodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testCheckRequestString) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
int64_t t = TC_Common::now2us();
TC_HttpRequest req;
int count = 100000;
int i = 0;
while(++i<count) {
ASSERT_TRUE(req.checkRequest(s.c_str(), s.size()));
}
ASSERT_TRUE(req.decode(s));
ASSERT_TRUE(req.getRequestUrl() == "/a/b");
cout << req.getURL().getDomain() << endl;
ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
cout << "testCheckRequestString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testCheckRequestBuff) //此时使用的是TEST_F宏
{
string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
+string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
+string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
+string("Accept-Encoding: gzip\r\n")
+string("Accept-Language: zh-cn,zh;q=0.5\r\n")
+string("Connection: close\r\n")
+string("Host: www.qq.com\r\n")
+string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
+string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
+string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
int64_t t = TC_Common::now2us();
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
TC_HttpRequest req;
int count = 100000;
int i = 0;
while(++i<count) {
ASSERT_TRUE(req.checkRequest(buff));
}
cout << "testCheckRequestBuff::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
}
TEST_F(HttpTest, testHttpFinish) //此时使用的是TEST_F宏
{
string body = "abdefghigk";
vector<string> sbuff;
sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Accept-Encoding: gzip\r\n");
sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header + body;
TC_HttpRequest request;
ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
}
TEST_F(HttpTest, testHttpFinishNoLength) //此时使用的是TEST_F宏
{
vector<string> sbuff;
sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Accept-Encoding: gzip\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header;
TC_HttpRequest request;
ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
}
TEST_F(HttpTest, testHttpNoFinish) //此时使用的是TEST_F宏
{
string body = "abdefghigk";
vector<string> sbuff;
sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Accept-Encoding: gzip\r\n");
sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header + body;
TC_HttpRequest request;
ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_LESS);
}
TEST_F(HttpTest, testHttpRequestChunked) //此时使用的是TEST_F宏
{
vector<string> body;
body.push_back("abasdfadefghiadfagk1");
body.push_back("abdasdfadfaefghigk2");
body.push_back("abdsaefghigk3");
body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
vector<string> sbuff;
sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Accept-Encoding: gzip\r\n");
sbuff.push_back("Transfer-Encoding: chunked\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
stringstream data;
for(auto s : sbuff)
{
data << s;
}
string sbody;
for(auto s : body)
{
sbody += s;
data << hex << s.size() << "\r\n" << s << "\r\n";
}
data << 0 << "\r\n\r\n";
string s = data.str();
TC_HttpRequest request;
ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
ASSERT_TRUE(request.decode(s));
ASSERT_TRUE(request.getContent() == sbody);
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
request.reset();
ASSERT_TRUE(request.checkRequest(buff));
}
TEST_F(HttpTest, testHttpRequestChunkedNoFinish) //此时使用的是TEST_F宏
{
vector<string> body;
body.push_back("abasdfadefghiadfagk1");
body.push_back("abdasdfadfaefghigk2");
body.push_back("abdsaefghigk3");
body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
vector<string> sbuff;
sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Accept-Encoding: gzip\r\n");
sbuff.push_back("Transfer-Encoding: chunked\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
stringstream data;
for(auto s : sbuff)
{
data << s;
}
data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
data << hex << body[2].size() ;
string s = data.str();
TC_HttpRequest request;
ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(!request.checkRequest(buff));
}
TEST_F(HttpTest, testHttpResponse) //此时使用的是TEST_F宏
{
string body = "abcdef";
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
sbuff.push_back("\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header + body;
{
TC_HttpResponse response;
ASSERT_TRUE(response.decode(s));
ASSERT_TRUE(response.getContent() == body);
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(response.incrementDecode(buff));
ASSERT_TRUE(response.getContent() == body);
}
}
TEST_F(HttpTest, testHttpResponseNoFinish) //此时使用的是TEST_F宏
{
string body = "abcdef";
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
sbuff.push_back("\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header + body;
{
TC_HttpResponse response;
ASSERT_TRUE(!response.decode(s));
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(!response.incrementDecode(buff));
}
}
TEST_F(HttpTest, testHttpResponseNoLength) //此时使用的是TEST_F宏
{
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Connection: close\r\n");
sbuff.push_back("\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header;
{
TC_HttpResponse response;
ASSERT_TRUE(response.decode(s));
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(response.incrementDecode(buff));
}
}
TEST_F(HttpTest, testHttpResponseIncrementFinish) //此时使用的是TEST_F宏
{
string body = "abcdeasdfadfsff";
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
sbuff.push_back("\r\n");
string header;
for(auto s : sbuff)
{
header += s;
}
string s = header;
{
TC_HttpResponse response;
ASSERT_TRUE(!response.decode(s));
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(!response.incrementDecode(buff));
}
s = header + body;
{
TC_HttpResponse response;
ASSERT_TRUE(response.decode(s));
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(response.incrementDecode(buff));
}
}
TEST_F(HttpTest, testHttpResponseChunked) //此时使用的是TEST_F宏
{
vector<string> body;
body.push_back("abdefghiasdfasdfsadfsadfsagk1");
body.push_back("abdefghasdfaaigk2");
body.push_back("abdefghigadsfadsfk3");
body.push_back("abdefgsfagasasdfasfdfdfsdfsfsdfdsffsdfsdfhigk4");
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Transfer-Encoding: chunked\r\n");
sbuff.push_back("Connection: close\r\n\r\n");
stringstream data;
for(auto s : sbuff)
{
data << s;
}
string sbody;
for(auto s : body)
{
sbody += s;
data << hex << s.size() << "\r\n" << s << "\r\n";
}
data << 0 << "\r\n\r\n";
string s = data.str();
{
TC_HttpResponse response;
ASSERT_TRUE(response.decode(s));
ASSERT_TRUE(response.getContent() == sbody);
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(response.incrementDecode(buff));
ASSERT_TRUE(response.getContent() == sbody);
}
}
TEST_F(HttpTest, testHttpResponseChunkedNoFinish) //此时使用的是TEST_F宏
{
vector<string> body;
body.push_back("abdefasdfasfasghigk1");
body.push_back("asdfaabdeafghigk2");
body.push_back("abasdfasdfasdfasdfasdfasdfasfasdefghigk3");
body.push_back("abdefgfasdfasdfasdfasdfadfigk4");
vector<string> sbuff;
sbuff.push_back("HTTP/1.1 200 OK\r\n");
sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
sbuff.push_back("Transfer-Encoding: chunked\r\n");
sbuff.push_back("Connection: close\r\n\r\n");
stringstream data;
for(auto s : sbuff)
{
data << s;
}
data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
data << hex << body[2].size() << "\r\n" << "abc";
string s = data.str();
{
TC_HttpResponse response;
ASSERT_TRUE(!response.decode(s));
}
{
TC_HttpResponse response;
TC_NetWorkBuffer buff(NULL);
buff.addBuffer(s.c_str(), s.size());
ASSERT_TRUE(!response.incrementDecode(buff));
}
}
TEST_F(HttpTest, testWeb) //此时使用的是TEST_F宏
{
string url = "www.qq.com";
TC_HttpRequest stHttpReq;
// stHttpReq.setCacheControl("no-cache");
// stHttpReq.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
stHttpReq.setHeader("Connection", "Close");
stHttpReq.setAcceptEncoding("gzip, deflate, br");
stHttpReq.setGetRequest(url);
string sSendBuffer = stHttpReq.encode();
TC_HttpResponse stHttpRsp;
int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
if(iRet != 0)
{
cout << iRet << endl;
}
string file = stHttpReq.getURL().getDomain() + ".html";
auto headers = stHttpRsp.getHeaders();
cout << "request:" << url << endl;
cout << TC_Common::tostr(headers.begin(), headers.end(), "\r\n") << endl;
TC_File::save2file(file, stHttpRsp.getContent());
// ASSERT_TRUE(stHttpRsp.getContentLength() == TC_File::getFileSize(file));
}
void testCookie(const string &sRspURL, const string &sReqURL, const vector<string> &vsCookie)
{
cout << sRspURL << "=>" << sReqURL << "-----------------------------------" << endl;
TC_HttpCookie cookie;
cookie.addCookie(sRspURL, vsCookie);
list<TC_HttpCookie::Cookie> vCookie = cookie.getAllCookie();
cout << "All Cookie:" << sRspURL << "-----------------------------------" << endl;
list<TC_HttpCookie::Cookie>::iterator it = vCookie.begin();
while(it != vCookie.end())
{
cout << TC_Common::tostr(it->_data.begin(), it->_data.end(), "; ") << ", " << it->_expires << ", " << it->_path << endl;
++it;
}
cout << "-----------------------------------" << endl << endl;
string sCookie;
cookie.getCookieForURL(sReqURL, sCookie);
cout << TC_Common::tostr(sCookie) << endl;
cout << "-----------------------------------" << endl << endl;
}
TEST_F(HttpTest, testCookie) //此时使用的是TEST_F宏
{
ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "www.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "www.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain("t.qq.com", "www.qq.com") == false);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "www.qq.com") == false);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "t.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "www.qq.com") == false);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "qq.com") == false);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".y.qq.com", "x.y.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".x.y.qq.com", "x.y.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "x.y.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "y.qq.com") == true);
ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "y.qq.com") == true);
cout << TC_Common::now2GMTstr() << endl;
string gmt = TC_Common::tm2GMTstr(time(NULL) + 10);
string s = "HTTP/1.1 200 OK\r\n";// 200 Aouut Error\r\n";
s += "Set-Cookie: n1=1; a=1; c=d; Path=/; Domain=qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n2=2; a=0; c=d; Path=/abc/def; Domain=.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n3=3; a=5; c=d; Path=/abc/def/aaa; Domain=.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n4=4; a=6; c=d; Path=/abc; Domain=.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n5=5; a=2; c=d; Path=/; Domain=.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n6=6; c=3; Path=/; Domain=y.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n7=7; c=3; Path=/abc; Domain=.y.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n8=6; c=3; Path=/; Domain=x.y.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n9=7; c=4; Path=/; Domain=.x.y.qq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n10=7; c=4; Path=/; Domain=qqq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n11=7; c=4; Path=/; Domain=.qqq.com; Expires=" + gmt + "\r\n";
s += "Set-Cookie: n12=8; c=4; Expires=" + gmt + "\r\n";
s += "Accept-Ranges: bytes\r\n\r\n";
TC_HttpResponse rsp;
rsp.decode(s);
cout << "-----------------------------------" << endl;
vector<string> vsCookie = rsp.getSetCookie();
cout << TC_Common::tostr(vsCookie.begin(), vsCookie.end(), "\r\n") << endl << endl;
testCookie("http://www.qq.com", "http://www.qq.com", vsCookie);
testCookie("http://www.qq.com/abc/def", "http://www.qq.com", vsCookie);
testCookie("http://www.qq.com/abc/def", "http://www.qq.com/abc", vsCookie);
cout << endl;
testCookie("http://www.qq.com", "http://qq.com", vsCookie);
testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com/abc/def/aaa", vsCookie);
testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com", vsCookie);
testCookie("http://www.qq.com", "http://www.qq.com/abc/def", vsCookie);
testCookie("http://qq.com", "http://qq.com/abc/def", vsCookie);
testCookie("http://qq.com", "http://t.qq.com/abc/def", vsCookie);
testCookie("http://qq.com", "http://y.qq.com/", vsCookie);
testCookie("http://qq.com", "http://y.qq.com/abc", vsCookie);
testCookie("http://x.y.qq.com", "http://x.y.qq.com", vsCookie);
}
//
//void Test_TC_Http::main()
//{
// testCheckRequestURL();
//
// testEncodeString();
// testEncodeVector();
// testEncodeBuffString();
//
// testDecodeString();
//
// testDecodeBuffString();
//
// testCheckRequestString();
// testCheckRequestBuff();
//
// testHttpFinishNoLength();
//
// testHttpFinish();
//
// testHttpNoFinish();
//
// testHttpRequestChunked();
//
// testHttpResponse();
//
// testHttpResponseNoFinish();
//
// testHttpResponseNoLength();
//
// testHttpResponseIncrementFinish();
//
// testHttpResponseChunked();
//
// testHttpRequestChunkedNoFinish();
//
// testHttpResponseChunkedNoFinish();
//
//// testWeb("http://www.baidu.com");
//// testWeb("http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx");
//// testWeb("http://www.qq.com");
//
//// testCookie();
//}

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
#define FLEX_SCANNER
#define YY_FLEX_MAJOR_VERSION 2
#define YY_FLEX_MINOR_VERSION 5
#define YY_FLEX_SUBMINOR_VERSION 37
#define YY_FLEX_SUBMINOR_VERSION 35
#if YY_FLEX_SUBMINOR_VERSION > 0
#define FLEX_BETA
#endif
@ -47,6 +47,7 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@ -54,6 +55,7 @@ typedef int flex_int32_t;
typedef unsigned char flex_uint8_t;
typedef unsigned short int flex_uint16_t;
typedef unsigned int flex_uint32_t;
#endif /* ! C99 */
/* Limits of integral types. */
#ifndef INT8_MIN
@ -84,8 +86,6 @@ typedef unsigned int flex_uint32_t;
#define UINT32_MAX (4294967295U)
#endif
#endif /* ! C99 */
#endif /* ! FLEXINT_H */
#ifdef __cplusplus
@ -176,7 +176,7 @@ extern FILE *yyin, *yyout;
*/
#define YY_LESS_LINENO(n) \
do { \
int yyl;\
yy_size_t yyl;\
for ( yyl = n; yyl < yyleng; ++yyl )\
if ( yytext[yyl] == '\n' )\
--yylineno;\
@ -369,7 +369,7 @@ static void yy_fatal_error (yyconst char msg[] );
*/
#define YY_DO_BEFORE_ACTION \
(yytext_ptr) = yy_bp; \
yyleng = (size_t) (yy_cp - yy_bp); \
yyleng = (yy_size_t) (yy_cp - yy_bp); \
(yy_hold_char) = *yy_cp; \
*yy_cp = '\0'; \
(yy_c_buf_p) = yy_cp;
@ -513,7 +513,7 @@ int yy_flex_debug = 0;
#define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET
char *yytext;
#line 1 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 1 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
/**
* Tencent is pleased to support the open source community by making Tars available.
*
@ -529,7 +529,7 @@ char *yytext;
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#line 20 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 20 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#include <map>
#include <string>
#include <sstream>
@ -649,7 +649,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)
#define ECHO fwrite( yytext, yyleng, 1, yyout )
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -660,7 +660,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
size_t n; \
yy_size_t n; \
for ( n = 0; n < max_size && \
(c = getc( yyin )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \
@ -742,7 +742,7 @@ YY_DECL
register char *yy_cp, *yy_bp;
register int yy_act;
#line 67 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 67 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
#line 749 "tars.lex.cpp"
@ -840,12 +840,12 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 69 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 69 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{ BEGIN(INCL); }
YY_BREAK
case 2:
YY_RULE_SETUP
#line 71 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 71 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if ( include_file_stack_ptr >= MAX_INCLUDE_DEPTH )
{
@ -878,7 +878,7 @@ YY_RULE_SETUP
YY_BREAK
case YY_STATE_EOF(INITIAL):
case YY_STATE_EOF(INCL):
#line 101 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 101 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
--include_file_stack_ptr;
if ( include_file_stack_ptr < 0 )
@ -897,14 +897,14 @@ case YY_STATE_EOF(INCL):
YY_BREAK
case 3:
YY_RULE_SETUP
#line 117 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 117 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
return TARS_SCOPE_DELIMITER;
}
YY_BREAK
case 4:
YY_RULE_SETUP
#line 121 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 121 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
// C++ comment
bool e = false;
@ -925,7 +925,7 @@ YY_RULE_SETUP
YY_BREAK
case 5:
YY_RULE_SETUP
#line 139 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 139 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
// C comment
bool e = false;
@ -976,7 +976,7 @@ YY_RULE_SETUP
YY_BREAK
case 6:
YY_RULE_SETUP
#line 187 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 187 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr ident = new StringGrammar;
ident->v = yytext;
@ -987,7 +987,7 @@ YY_RULE_SETUP
case 7:
/* rule 7 can match eol */
YY_RULE_SETUP
#line 194 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 194 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr ident = new StringGrammar;
ident->v = yytext;
@ -1000,7 +1000,7 @@ YY_RULE_SETUP
YY_BREAK
case 8:
YY_RULE_SETUP
#line 204 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 204 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
StringGrammarPtr str = new StringGrammar;
bool e = false;
@ -1115,7 +1115,7 @@ YY_RULE_SETUP
YY_BREAK
case 9:
YY_RULE_SETUP
#line 316 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 316 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
errno = 0;
IntergerGrammarPtr ptr = new IntergerGrammar;
@ -1140,7 +1140,7 @@ YY_RULE_SETUP
YY_BREAK
case 10:
YY_RULE_SETUP
#line 338 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 338 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
errno = 0;
FloatGrammarPtr ptr = new FloatGrammar;
@ -1175,7 +1175,7 @@ YY_RULE_SETUP
case 11:
/* rule 11 can match eol */
YY_RULE_SETUP
#line 369 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 369 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if(yytext[0] == '\n')
{
@ -1185,7 +1185,7 @@ YY_RULE_SETUP
YY_BREAK
case 12:
YY_RULE_SETUP
#line 376 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 376 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
{
if(yytext[0] < 32 || yytext[0] > 126)
{
@ -1204,7 +1204,7 @@ YY_RULE_SETUP
YY_BREAK
case 13:
YY_RULE_SETUP
#line 392 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 392 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"
ECHO;
YY_BREAK
#line 1211 "tars.lex.cpp"
@ -1398,7 +1398,7 @@ static int yy_get_next_buffer (void)
{ /* Not enough room in the buffer - grow it. */
/* just a shorter name for the current buffer */
YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE;
YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
int yy_c_buf_p_offset =
(int) ((yy_c_buf_p) - b->yy_ch_buf);
@ -1531,7 +1531,7 @@ static int yy_get_next_buffer (void)
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
yy_is_jam = (yy_current_state == 51);
return yy_is_jam ? 0 : yy_current_state;
return yy_is_jam ? 0 : yy_current_state;
}
static void yyunput (int c, register char * yy_bp )
@ -1623,7 +1623,7 @@ static int yy_get_next_buffer (void)
case EOB_ACT_END_OF_FILE:
{
if ( yywrap( ) )
return EOF;
return 0;
if ( ! (yy_did_buffer_switch_on_eof) )
YY_NEW_FILE;
@ -1764,6 +1764,10 @@ static void yy_load_buffer_state (void)
yyfree((void *) b );
}
#ifndef __cplusplus
extern int isatty (int );
#endif /* __cplusplus */
/* Initializes or reinitializes a buffer.
* This function is sometimes called more than once on the same buffer,
* such as during a yyrestart() or at EOF.
@ -1968,8 +1972,8 @@ YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
/** Setup the input buffer state to scan the given bytes. The next call to yylex() will
* scan from a @e copy of @a bytes.
* @param yybytes the byte buffer to scan
* @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes.
* @param bytes the byte buffer to scan
* @param len the number of bytes in the buffer pointed to by @a bytes.
*
* @return the newly allocated buffer state object.
*/
@ -1977,8 +1981,7 @@ YY_BUFFER_STATE yy_scan_bytes (yyconst char * yybytes, yy_size_t _yybytes_len
{
YY_BUFFER_STATE b;
char *buf;
yy_size_t n;
yy_size_t i;
yy_size_t n, i;
/* Get memory for full buffer, including space for trailing EOB's. */
n = _yybytes_len + 2;
@ -2211,7 +2214,7 @@ void yyfree (void * ptr )
#define YYTABLES_NAME "yytables"
#line 392 "/home/jarod/TarsFramework/tarscpp/tools/tarsgrammar/tars.l"
#line 392 "/Volumes/MyData/centos/TarsCloud/framework/tarscpp/tools/tarsgrammar/tars.l"

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,14 @@
/* A Bison parser, made by GNU Bison 3.0.4. */
/* A Bison parser, made by GNU Bison 2.3. */
/* Bison interface for Yacc-like parsers in C
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -15,7 +16,9 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
@ -30,65 +33,87 @@
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_YY_TARS_TAB_HPP_INCLUDED
# define YY_YY_TARS_TAB_HPP_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 1
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Token type. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
TARS_VOID = 258,
TARS_STRUCT = 259,
TARS_BOOL = 260,
TARS_BYTE = 261,
TARS_SHORT = 262,
TARS_INT = 263,
TARS_DOUBLE = 264,
TARS_FLOAT = 265,
TARS_LONG = 266,
TARS_STRING = 267,
TARS_VECTOR = 268,
TARS_MAP = 269,
TARS_NAMESPACE = 270,
TARS_INTERFACE = 271,
TARS_IDENTIFIER = 272,
TARS_OUT = 273,
TARS_OP = 274,
TARS_KEY = 275,
TARS_ROUTE_KEY = 276,
TARS_REQUIRE = 277,
TARS_OPTIONAL = 278,
TARS_CONST_INTEGER = 279,
TARS_CONST_FLOAT = 280,
TARS_FALSE = 281,
TARS_TRUE = 282,
TARS_STRING_LITERAL = 283,
TARS_SCOPE_DELIMITER = 284,
TARS_CONST = 285,
TARS_ENUM = 286,
TARS_UNSIGNED = 287,
BAD_CHAR = 288
};
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
TARS_VOID = 258,
TARS_STRUCT = 259,
TARS_BOOL = 260,
TARS_BYTE = 261,
TARS_SHORT = 262,
TARS_INT = 263,
TARS_DOUBLE = 264,
TARS_FLOAT = 265,
TARS_LONG = 266,
TARS_STRING = 267,
TARS_VECTOR = 268,
TARS_MAP = 269,
TARS_NAMESPACE = 270,
TARS_INTERFACE = 271,
TARS_IDENTIFIER = 272,
TARS_OUT = 273,
TARS_OP = 274,
TARS_KEY = 275,
TARS_ROUTE_KEY = 276,
TARS_REQUIRE = 277,
TARS_OPTIONAL = 278,
TARS_CONST_INTEGER = 279,
TARS_CONST_FLOAT = 280,
TARS_FALSE = 281,
TARS_TRUE = 282,
TARS_STRING_LITERAL = 283,
TARS_SCOPE_DELIMITER = 284,
TARS_CONST = 285,
TARS_ENUM = 286,
TARS_UNSIGNED = 287,
BAD_CHAR = 288
};
#endif
/* Tokens. */
#define TARS_VOID 258
#define TARS_STRUCT 259
#define TARS_BOOL 260
#define TARS_BYTE 261
#define TARS_SHORT 262
#define TARS_INT 263
#define TARS_DOUBLE 264
#define TARS_FLOAT 265
#define TARS_LONG 266
#define TARS_STRING 267
#define TARS_VECTOR 268
#define TARS_MAP 269
#define TARS_NAMESPACE 270
#define TARS_INTERFACE 271
#define TARS_IDENTIFIER 272
#define TARS_OUT 273
#define TARS_OP 274
#define TARS_KEY 275
#define TARS_ROUTE_KEY 276
#define TARS_REQUIRE 277
#define TARS_OPTIONAL 278
#define TARS_CONST_INTEGER 279
#define TARS_CONST_FLOAT 280
#define TARS_FALSE 281
#define TARS_TRUE 282
#define TARS_STRING_LITERAL 283
#define TARS_SCOPE_DELIMITER 284
#define TARS_CONST 285
#define TARS_ENUM 286
#define TARS_UNSIGNED 287
#define BAD_CHAR 288
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef int YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif
extern YYSTYPE yylval;
int yyparse (void);
#endif /* !YY_YY_TARS_TAB_HPP_INCLUDED */

View File

@ -1,20 +1,4 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "util/tc_http.h"
#include "util/tc_http.h"
#include "util/tc_port.h"
#include "util/tc_common.h"
#include "util/tc_clientsocket.h"
@ -174,7 +158,7 @@ string TC_URL::getRequest() const
bool TC_URL::parseURL(const string &originRequest)
{
// string originRequest = TC_Common::trim(sURL, " ");
// string originRequest = TC_Common::trim(sURL, " ");
if (originRequest.empty())
{
@ -225,8 +209,6 @@ bool TC_URL::parseURL(const string &originRequest)
{
sUrlAuthority = originRequest.substr(iPos, nQuestionIndex - iPos);
// string sTemp = originRequest.substr(nQuestionIndex);
sUrlPath += '/';
sUrlPath += originRequest.substr(nQuestionIndex);
}
@ -234,8 +216,6 @@ bool TC_URL::parseURL(const string &originRequest)
{
sUrlAuthority = originRequest.substr(iPos, nNumbersignIndex - iPos);
// string sTemp = originRequest.substr(nNumbersignIndex);
sUrlPath += '/';
sUrlPath += originRequest.substr(nNumbersignIndex);
}
@ -1116,81 +1096,6 @@ void TC_HttpCookie::deleteExpires(time_t t, bool bErase)
}
/********************* TC_HttpResponse ***********************/
//
//void TC_HttpResponse::parseResponseHeader(const char* szBuffer, const char* header)
//{
// auto it = strstr(szBuffer, "\r\n");
//
// assert(it != NULL);
//
// string sep = " ";
//
// auto f1 = std::search(szBuffer, it, sep.c_str(), sep.c_str() + sep.size());
// if(f1 == it)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response parse version format error : " + string(szBuffer, it-szBuffer));
// }
//
// auto f2 = std::search(f1 + 1, it, sep.c_str(), sep.c_str() + sep.size());
// if(f1 == it)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response parse status format error : " + string(szBuffer, it-szBuffer));
// }
//
// _headerLine = string(szBuffer, it-szBuffer);
//
// if(TC_Port::strncasecmp(_headerLine.c_str(), "HTTP/", 5) != 0)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response version is not start with 'HTTP/' : " + _headerLine);
// }
//
// _version = string(szBuffer, f1);
//
// _status = TC_Common::strto<int>(string(f1 + 1, f2));
//
// _about = TC_Common::trim(string(f2 + 1, it));
//
// parseHeader(szBuffer, header, _headers);
//}
//
//void TC_HttpResponse::parseResponseHeader(TC_NetWorkBuffer &buff, TC_NetWorkBuffer::buffer_iterator &headerIt)
//{
// string line = "\r\n";
// auto it = buff.find(line.c_str(), line.size());
//
// assert(it != buff.end());
//
// string sep = " ";
//
// auto f1 = std::search(buff.begin(), it, sep.c_str(), sep.c_str() + sep.size());
// if(f1 == it)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response parse version format error : " + buff.iteratorToIterator<string>(buff.begin(), it));
// }
//
// auto f2 = std::search(f1 + 1, it, sep.c_str(), sep.c_str() + sep.size());
// if(f1 == it)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response parse status format error : " + buff.iteratorToIterator<string>(buff.begin(), it));
// }
//
// _headerLine = buff.iteratorToIterator<string>(buff.begin(), it);
//
// if(TC_Port::strncasecmp(_headerLine.c_str(), "HTTP/", 5) != 0)
// {
// throw TC_HttpResponse_Exception("[TC_HttpResponse_Exception::parseResponeHeader] http response version is not start with 'HTTP/' : " + _headerLine);
// }
//
// _version = buff.iteratorToIterator<string>(buff.begin(), f1);
//
// _status = TC_Common::strto<int>(buff.iteratorToIterator<string>(f1 + 1, f2));
//
// _about = TC_Common::trim(buff.iteratorToIterator<string>(f2 + 1, it));
//
// parseHeader(buff.begin(), headerIt, _headers);
//
//}
void TC_HttpResponse::reset()
{
TC_Http::reset();
@ -1277,24 +1182,13 @@ bool TC_HttpResponse::incrementDecode(TC_NetWorkBuffer &buff)
_headLength = p - data.first + 4;
_iTmpContentLength = parseResponseHeaderString(data.first, data.first + data.second + 2);
_iTmpContentLength = parseResponseHeaderString(data.first, data.first + data.second);
//304的返回码中头里本来就没有Content-Length也不会有数据体头收全了就是真正的收全了
if ( (204 == _status) || (304 == _status) )
{
return true;
}
//
// http_header_type::const_iterator it = _headers.find("Content-Length");
// if (it != _headers.end())
// {
// _iTmpContentLength = getContentLength();
// }
// else
// {
// //没有指明ContentLength, 接收到服务器关闭连接
// _iTmpContentLength = -1;
// }
buff.moveHeader(_headLength);
@ -1303,11 +1197,9 @@ bool TC_HttpResponse::incrementDecode(TC_NetWorkBuffer &buff)
{
return true;
}
// return true;
//是否是chunk编码
_bIsChunked = checkHeader("Transfer-Encoding", "chunked");
// _bIsChunked = (getHeader("Transfer-Encoding") == "chunked");
if(_bIsChunked) {
//删除头部里面
eraseHeader("Transfer-Encoding");
@ -1366,7 +1258,9 @@ bool TC_HttpResponse::incrementDecode(TC_NetWorkBuffer &buff)
buff.clearBuffers();
setContentLength(_iRecvContentLength);
if(_iRecvContentLength > 0) {
setContentLength(_iRecvContentLength);
}
return true;
}
@ -1383,7 +1277,9 @@ bool TC_HttpResponse::incrementDecode(TC_NetWorkBuffer &buff)
buff.clearBuffers();
setContentLength(_iRecvContentLength);
if(_iRecvContentLength > 0) {
setContentLength(_iRecvContentLength);
}
return true;
}
@ -1414,14 +1310,15 @@ bool TC_HttpResponse::decode(const char *sBuffer, size_t iLength)
{
assert(sBuffer != NULL);
const char *p = strstr(sBuffer, "\r\n\r\n");
const char *pHeader = sBuffer;
const char *p = strnstr(sBuffer, "\r\n\r\n", iLength);
if ( p == NULL)
{
return false;
}
//解析头部
// parseResponseHeader(sBuffer, p + 2);
_iTmpContentLength = parseResponseHeaderString(sBuffer, p + 2);
//304的返回码中头里本来就没有Content-Length也不会有数据体头收全了就是真正的收全了
@ -1429,17 +1326,6 @@ bool TC_HttpResponse::decode(const char *sBuffer, size_t iLength)
{
return true;
}
//
// http_header_type::const_iterator it = _headers.find("Content-Length");
// if (it != _headers.end())
// {
// _iTmpContentLength = getContentLength();
// }
// else
// {
// //没有指明ContentLength, 接收到服务器关闭连接
// _iTmpContentLength = -1;
// }
_headLength = p - sBuffer + 4;
@ -1467,7 +1353,7 @@ bool TC_HttpResponse::decode(const char *sBuffer, size_t iLength)
{
while (true)
{
p = strstr(sBuffer, "\r\n");
p = strnstr(sBuffer, "\r\n", iLength - (sBuffer - pHeader));
if (p == NULL)
return false;
@ -1632,40 +1518,6 @@ void TC_HttpRequest::reset()
_httpURL.clear();
}
//
//const char * TC_HttpRequest::requestType2str(int iRequestType) const
//{
// if(iRequestType == REQUEST_GET)
// {
// return "GET";
// }
// else if(iRequestType == REQUEST_HEAD)
// {
// return "HEAD";
// }
// else if(iRequestType == REQUEST_POST)
// {
// return "POST";
// }
// else if(iRequestType == REQUEST_OPTIONS)
// {
// return "OPTIONS";
// }
// else if(iRequestType == REQUEST_PUT)
// {
// return "PUT";
// }
// else if(iRequestType == REQUEST_DELETE)
// {
// return "DELETE";
// }
// else if(iRequestType == REQUEST_PATCH)
// {
// return "PATCH";
// }
//// assert(true);
// return "";
//}
vector<string> TC_HttpRequest::getCookie()
{
@ -1700,9 +1552,6 @@ void TC_HttpRequest::parseURL(const string& sUrl)
void TC_HttpRequest::setRequest(const string& method, const string &sUrl, const std::string& body, bool bNewCreateHost)
{
// std::string lowMethod(method);
// std::transform(method.begin(), method.end(), lowMethod.begin(), ::tolower);
if (TC_Port::strncasecmp(method.c_str(), "GET", 3) == 0)
setGetRequest(sUrl, bNewCreateHost);
else if (TC_Port::strncasecmp(method.c_str(), "HEAD", 4) == 0)
@ -1922,7 +1771,7 @@ bool TC_HttpRequest::decode(const char *sBuffer, size_t iLength)
throw runtime_error("[TC_HttpRequest::decode] protocol not support ");
}
p = strstr(sBuffer, "\r\n\r\n");
p = strnstr(sBuffer, "\r\n\r\n", iLength);
if (p == NULL)
{
return false;
@ -1941,18 +1790,16 @@ bool TC_HttpRequest::decode(const char *sBuffer, size_t iLength)
if (bChunk)
{
p = sBuffer + _headLength;
// string sTmp(sBuffer + _headLength, iLength - _headLength);
while (true)
{
// string::size_type pos = sTmp.find("\r\n");
const char *pos = strstr(p, "\r\n");
const char *pos = strnstr(p, "\r\n", iLength - (p - sBuffer));
// const char *pos = strstr(p, "\r\n");
if (pos == NULL)
return false;
//查找当前chunk的大小
// string sChunkSize = sTmp.substr(0, pos);
string sChunkSize(p, pos - p);
// sTmp.substr(0, pos);
int iChunkSize = strtol(sChunkSize.c_str(), NULL, 16);
iChunkSuffixLen = iChunkSuffixLen + sChunkSize.length();
@ -1961,15 +1808,12 @@ bool TC_HttpRequest::decode(const char *sBuffer, size_t iLength)
iChunkSuffixLen = iChunkSuffixLen + 4;
break; //所有chunk都接收完毕
}
// if (sTmp.length() >= pos + 2 + (size_t)iChunkSize + 2) //接收到一个完整的chunk了
if ((sBuffer+iLength-p) >= (pos - p) + 2 + iChunkSize + 2) //接收到一个完整的chunk了
{
//获取一个chunk的内容
// _content += sTmp.substr(pos + 2, iChunkSize);
_content.append(pos + 2, iChunkSize);
//删除一个chunk
// sTmp = sTmp.substr(pos + 2 + iChunkSize + 2);
p = pos + 2 + iChunkSize + 2;
iChunkSuffixLen = iChunkSuffixLen + 4;
}
@ -1995,193 +1839,6 @@ bool TC_HttpRequest::checkRequest(TC_NetWorkBuffer &buff)
buff.mergeBuffers();
return checkRequest(buff.getBufferPointer().first, buff.getBufferLength());
//
// size_t *headerLength = (size_t *)buff.getContextData();
//
// if(headerLength == NULL)
// {
// headerLength = new size_t(0);
//
// buff.setContextData(headerLength, [=]{ delete headerLength;});
// }
//
// if(*headerLength == 0)
// {
// //header还没有解析出来, 合并buffer
// buff.mergeBuffers();
//
// auto data = buff.getBufferPointer();
//
// const char *p = strnstr(data.first, " ", 10);
// if(p == NULL)
// {
// throw runtime_error("[TC_HttpRequest::checkRequest] http protocol parse error");
// }
//
// auto it = TC_Http::HEADER.find(string(data.first, p - data.first));
// if(it == TC_Http::HEADER.end())
// {
// throw runtime_error("[TC_HttpRequest::checkRequest] protocol not support");
// }
//
//// if (buff.getBufferLength() < 10)
//// return false;
//
//// auto data = buff.getBufferPointer();
//// if (TC_Port::strncasecmp(data.first, "GET ", 4) != 0 &&
//// TC_Port::strncasecmp(data.first, "POST ", 5) != 0 &&
//// TC_Port::strncasecmp(data.first, "PUT ", 4) != 0 &&
//// TC_Port::strncasecmp(data.first, "PATCH ", 6) != 0 &&
//// TC_Port::strncasecmp(data.first, "OPTIONS ", 8) != 0 &&
//// TC_Port::strncasecmp(data.first, "PRI ", 4) != 0 &&
//// TC_Port::strncasecmp(data.first, "DELETE ", 7) != 0 &&
//// TC_Port::strncasecmp(data.first, "HEAD ", 5)) {
//// throw runtime_error(
//// "[TC_HttpRequest::decode] protocol not support, only support GET HEAD POST PUT PATCH DELETE and OPTIONS ");
//// }
//
//// string sep = "\r\n\r\n";
////
//// auto sit = buff.find(sep.c_str(), sep.size());
//// if (sit == buff.end()) {
//// return false;
//// }
//
// p = strnstr(data.first, "\r\n\r\n", data.second);
// if(p == NULL) {
// return false;
// }
//
//// size_t pos = p ;
//
// *headerLength = p - data.first + 4;
//
//// bool bChunk = false;
//
//// sep = "\r\n";
//
// p = strnstr(data.first, "\r\n", data.second);
//
// assert(p != NULL);
//// if(p == NULL) {
//// return false;
//// }
////
//// auto it = std::search(buff.begin(), buff.end(), sep.c_str(), sep.c_str() + sep.size());
//// if (it == buff.end()) {
//// //first line
//// return false;
//// }
//
//// it = it + sep.size();
// //move to next line
// p += 2;
// }
//
// bool bChunk = false;
//
// size_t len = 0;
//
// //找到\r\n\r\n之前的长度表示
// while (true)
// {
// size_t iMoveLen = it - buff.begin();
// if (iMoveLen >= iHeadLen)
// {
// break;
// }
//
// auto lineItStart = it;
//
// auto lineItEnd = std::search(lineItStart, buff.end(), sep.c_str(), sep.c_str() + sep.size());
//
// if (lineItEnd != buff.end())
// {
// it = lineItEnd + sep.size();
//
// std::function<bool(char, char)> cmp = [](char i, char j)
// {
// return toupper(i) == toupper(j);
// };
//
// const char *TE = "Transfer-Encoding:";
//
// auto itTE = std::search(lineItStart, lineItEnd, TE, TE + strlen(TE), cmp);
//
// if (itTE != lineItEnd)
// {
// const char *CH = "chunked";
//
// auto itCH = std::search(lineItStart + strlen(TE), lineItEnd, CH, CH + strlen(CH), cmp);
//
// if (itCH != lineItEnd)
// {
// bChunk = true;
// break;
// }
// }
//
// const char *CL = "Content-Length:";
//
// auto itCL = std::search(lineItStart, lineItEnd, CL, CL + strlen(CL), cmp);
// if (itCL == lineItEnd)
// {
// continue;
// }
//
// string contentLength;
// contentLength.resize(lineItEnd - lineItStart - strlen(CL));
// std::copy(lineItStart + strlen(CL), lineItEnd, contentLength.begin());
//
// len = taf::TC_Common::strto<size_t>(TC_Common::trim(contentLength, " "));
// }
//
// }
//
// if (bChunk)
// {
// int remain_len = buff.getBufferLength() - iHeadLen;
// int move_len = 0;
//
// auto it = sit + 4;
// while (true)
// {
// auto lineIt = std::search(it, buff.end(), sep.c_str(), sep.c_str() + sep.size());
// if ( lineIt == buff.end() )
// {
// return false;
// }
//
// //查找当前chunk的大小
// string contentLength;
// contentLength.resize(lineIt - it);
// std::copy(it, lineIt, contentLength.begin());
//
// int iChunkSize = strtol(contentLength.c_str(), NULL, 16);
// if (iChunkSize <= 0)
// {
// return true; //所有chunk都接收完毕
// }
//
// move_len = (lineIt - it) + 2 + iChunkSize + 2;
// if ( remain_len >= move_len ) //接收到一个完整的chunk了
// {
// //移动到下一个chunk
// remain_len -= move_len;
// it = lineIt + 2 + iChunkSize + 2;
// }
// else
// {
// return false;
// }
// }
// }
// else if (len + pos + 4 <= buff.getBufferLength())
// {
// return true;
// }
//
// return false;
}
bool TC_HttpRequest::checkRequest(const char* sBuffer, size_t iLen)
@ -2200,20 +1857,8 @@ bool TC_HttpRequest::checkRequest(const char* sBuffer, size_t iLen)
{
throw runtime_error("[TC_HttpRequest::checkRequest] protocol not support: " + string(sBuffer, 8));
}
//
// if(TC_Port::strncasecmp(sBuffer, "GET " ,4) !=0 &&
// TC_Port::strncasecmp(sBuffer, "POST " ,5) !=0 &&
// TC_Port::strncasecmp(sBuffer, "PUT " ,4) !=0 &&
// TC_Port::strncasecmp(sBuffer, "PATCH " ,6) !=0 &&
// TC_Port::strncasecmp(sBuffer, "OPTIONS " ,8) !=0 &&
// TC_Port::strncasecmp(sBuffer, "PRI " , 4) !=0 &&
// TC_Port::strncasecmp(sBuffer, "DELETE " , 7) !=0 &&
// TC_Port::strncasecmp(sBuffer, "HEAD " ,5))
// {
// throw runtime_error("[TC_HttpRequest::decode] protocol not support:" + string(sBuffer, 8));
// }
const char *header = strstr(sBuffer, "\r\n\r\n");
const char *header = strnstr(sBuffer, "\r\n\r\n", iLen);
if ( header == NULL)
{
return false;
@ -2223,7 +1868,7 @@ bool TC_HttpRequest::checkRequest(const char* sBuffer, size_t iLen)
size_t iHeadLen = pos + 4;
p = strstr(sBuffer, "\r\n");
p = strnstr(sBuffer, "\r\n", iHeadLen);
if(p == NULL)
{
//first line
@ -2246,7 +1891,9 @@ bool TC_HttpRequest::checkRequest(const char* sBuffer, size_t iLen)
}
const char *line = p;
const char *lineEnd = strstr(line, "\r\n");
// const char *lineEnd = strnstr(line, "\r\n", iLen - (line - sBuffer));
const char *lineEnd = strstr(line, "\r\n");
if (lineEnd != NULL)
{
p = lineEnd + 2;
@ -2275,7 +1922,9 @@ bool TC_HttpRequest::checkRequest(const char* sBuffer, size_t iLen)
const char * pCur = header + 4;
while (true)
{
p = strstr(pCur , "\r\n");
p = strnstr(pCur, "\r\n", remain_len);
// p = strstr(pCur , "\r\n");
if ( p == NULL )
{
return false;
@ -2317,15 +1966,15 @@ size_t TC_Http::parseHeaderString(const char *beginIt, const char *headerIt, TC_
bool first = true;
auto lineStartIt= beginIt;
bool isLastLine = false;
bool isLastLine = false;
while (true)
{
auto it = strnstr(lineStartIt, "\r\n", headerIt - lineStartIt);
if(it == NULL)
{
it = headerIt;
isLastLine = true;
it = headerIt;
isLastLine = true;
}
//first line ignore
@ -2358,6 +2007,10 @@ size_t TC_Http::parseHeaderString(const char *beginIt, const char *headerIt, TC_
contentLength = TC_Common::strto<size_t>(value);
}
if(name.empty())
{
assert(false);
}
sHeader.insert(multimap<string, string>::value_type(std::move(name), std::move(value)));
}
}
@ -2366,10 +2019,10 @@ size_t TC_Http::parseHeaderString(const char *beginIt, const char *headerIt, TC_
first = false;
}
if (isLastLine)
{
break;
}
if (isLastLine)
{
break;
}
lineStartIt = it + 2;//sep.size();
}
@ -2383,15 +2036,12 @@ void TC_HttpRequest::parseRequestHeader(const char* szBuffer, const char *header
assert(p != NULL);
// string sep = " ";
// auto f1 = std::search(szBuffer, p, sep.c_str(), sep.c_str() + sep.size());
auto f1 = strnstr(szBuffer, " ", p - szBuffer);
if (f1 == p)
{
throw TC_HttpRequest_Exception("[TC_HttpRequest::parseRequestHeader] http request format error: " + string(szBuffer, p - szBuffer));
}
// auto f2 = std::search(f1 + 1, p, sep.c_str(), sep.c_str() + sep.size());
auto f2 = strnstr(f1 + 1, " ", p - f1 + 1);//std::search(f1 + 1, p, sep.c_str(), sep.c_str() + sep.size());
if (f2 == p || f1 >= f2)
{

View File

@ -35,9 +35,9 @@ bool TC_SpinLock::tryLock() const
int trys = 100;
for (; trys > 0 && _flag.test_and_set(std::memory_order_acquire); --trys)
{
#if TARGET_PLATFORM_LINUX
asm volatile("rep; nop" ::: "memory");
#endif
//#if TARGET_PLATFORM_LINUX
// asm volatile("rep; nop" ::: "memory");
//#endif
}