本文整理汇总了C++中HttpResponse::encode方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpResponse::encode方法的具体用法?C++ HttpResponse::encode怎么用?C++ HttpResponse::encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpResponse
的用法示例。
在下文中一共展示了HttpResponse::encode方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doStuff
// Handle a connection from a client
void doStuff(int socketFd) {
// Get the request
std::string request = "";
char buf[8193] = {0};
if(recv(socketFd, buf, 8192, 0) > 0) {
request.append(buf);
}
// Parse the request
HttpResponse response;
// Get the url from request
size_t start = request.find(' ');
size_t end = request.find(' ', start + 1);
std::string version = request.substr(end + 1, 8);
HttpRequest req;
if (request != "") {
req.consume(request);
}
if(req.invalidRequest() || version.substr(0, 4) != "HTTP") {
response.setStatus("400 Invalid Request");
}
else if ("HTTP/1.0" != version) {
response.setStatus("505 Version not supported");
}
else {
std::string url = request.substr(start + 1, end - start - 1);
response.setUrl(directory + "/" + url);
response.setMessage();
}
std::string blob = response.encode();
// Form a response
// Send back the response
send(socketFd, blob.c_str(), blob.length() + 1, 0);
}
示例2: receiveRequest
void receiveRequest(int clientSockfd, string dir){
// read/write data from/into the connection
bool isEnd = false;
char buf[1000] = { 0 };
stringstream ssOverall;
stringstream ssIteration;
const string endingStr = "\r\n\r\n";
unsigned int endingCount = 0;
while (!isEnd) {
memset(buf, '\0', sizeof(buf));
if (recv(clientSockfd, buf, 1000, 0) == -1) {
perror("recv");
return;// 5;
}
ssOverall << buf;
ssIteration << buf;
string currString = ssIteration.str();
for(unsigned int i = 0; i < currString.length(); i++){
if(currString[i] == endingStr[endingCount])
endingCount++;
else
endingCount = 0;
if(endingCount == 4){
string totalReqString = ssOverall.str();
// cout << "--------totalReqString--------" << endl << totalReqString << endl;
vector<uint8_t> decoded(totalReqString.begin(), totalReqString.end());
HttpRequest req = HttpRequest::decode((ByteBlob)decoded);
HttpResponse resp = processRequest(req, dir); //Process the request object
ByteBlob respBB = resp.encode();
uint8_t* respBytes = &respBB[0];
int respBytesSize = sizeof(uint8_t) * respBB.size();
// cout << "Num bytes being sent total: " << respBB.size() << endl;
// cout << "Num bytes being sent, data: " << resp.getData().size() << endl;
std::ofstream os("asdfasdfasdf.jpg");
if (!os) {
std::cerr<<"Error writing to ..."<<std::endl;
}
else {
HttpResponse decodedResp = HttpResponse::decode(respBB);
// ByteBlob data = resp.getData();
ByteBlob data = decodedResp.getData();
for(ByteBlob::iterator x=data.begin(); x<data.end(); x++){
os << *x;
}
os.close();
}
if (send(clientSockfd, respBytes, respBytesSize, 0) == -1) {
perror("send");
return;// 6;
}
//ssOverall.str(""); doesn't matter, we're closing connection
//endingCount = 0;
isEnd = true;
break;
}
}
}
close(clientSockfd);
cout << "Server closing" << endl;;
}
示例3: do_child
//.........这里部分代码省略.........
set_status = true;
}
}
else if(set_status == false)
{
std::ifstream file;
file.exceptions(
std::ifstream::badbit
| std::ifstream::eofbit);
file.open(filepath, std::ifstream::in | std::ifstream::binary | std::ifstream::out);
if (!file.is_open())
{
status.m_statuscode = "404";
response.setStatus(status);
response.setDescription ("Not Found");
response.setHeader("Content-Length", "0");
set_status = true;
}
else
{
status.m_statuscode = "200";
response.setStatus(status);
response.setDescription ("OK");
set_status = true;
file.seekg(0, std::ios::end);
std::streampos length(file.tellg());
if (length) {
file.seekg(0, std::ios::beg);
pload.resize(static_cast<std::size_t>(length));
file.read(&pload.front(), static_cast<std::size_t>(length));
}
file.close();
response.setPayLoad(pload);
std::stringstream out;
p_size = pload.size();
out << p_size;
response.setHeader("Content-Length", out.str());
}
memset(filepath, '\0', 100);
}
res_wire = response.encode();
int wsize = res_wire.size();
memset(buf, '\0', sizeof(buf));
for (int k1 = 0; k1 < wsize; k1++) {
buf[k1] = res_wire[k1];
}
res_wire.clear();
if (send(fd, buf, wsize, 0) == -1) {
perror("send");
exit(1);
}
if(response.getStatus().m_statuscode == "200")
{
int divide = p_size/500;
int remainder = p_size % 500;
int count = 0;
for (int k = 0; k < divide; k++) {
memset(buf, '\0', sizeof(buf));
for (int k1 = 0; k1 < 500; k1++) {
buf[k1] = pload[count];
count ++;
}
if (send(fd, buf, 500, 0) == -1) {
perror("send");
exit(1);
}
}
if(remainder!=0)
{
memset(buf, '\0', sizeof(buf));
for(int q = 0; q < remainder; q++)
{
buf[q] = pload[count];
count++;
}
if (send(fd, buf, remainder, 0) == -1) {
perror("send");
exit(1);
}
}
}
pload.clear();
if(response.getMap("Connection") == "close")
{
std::cout << "successfully close the connection" <<std::endl;
close(fd);
break;
}
}
}