码迷,mamicode.com
首页 > Web开发 > 详细

muduo源码-HttpRequest.h

时间:2018-03-29 12:44:11      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:std   ali   assert   const   源码   second   include   san   bool   

1 设计

HttpRequest 类,是解析完毕的请求体所存放的地方,被关联在 HttpConnext中.

 

2 源码

#ifndef MUDUO_NET_HTTP_HTTPREQUEST_H
#define MUDUO_NET_HTTP_HTTPREQUEST_H

#include <muduo/base/copyable.h>
#include <muduo/base/Timestamp.h>
#include <muduo/base/Types.h>

#include <map>
#include <assert.h>
#include <stdio.h>

namespace muduo
{
namespace net
{

// HttpContext 类只是负责解析的,请求的
// 解析出来的数据是存放在 HttpRequest 类中的
class HttpRequest : public muduo::copyable
{
 public:
//  解析出来的 请求方法
  enum Method
  {
    kInvalid, kGet, kPost, kHead, kPut, kDelete
  };
  // http版本
  enum Version
  {
    kUnknown, kHttp10, kHttp11
  };

  HttpRequest()
    : method_(kInvalid),
      version_(kUnknowkExpectBodyn)
  {
  }

  // 设置的
  void setVersion(Version v)
  {
    version_ = v;
  }

  Version getVersion() const
  { return version_; }

  bool setMethod(const char* start, const char* end)
  {
    assert(method_ == kInvalid);
    string m(start, end);
    if (m == "GET")
    {
      method_ = kGet;
    }
    else if (m == "POST")
    {
      method_ = kPost;
    }
    else if (m == "HEAD")
    {
      method_ = kHead;
    }
    else if (m == "PUT")
    {
      method_ = kPut;
    }
    else if (m == "DELETE")
    {
      method_ = kDelete;
    }
    else
    {
      method_ = kInvalid;
    }
    return method_ != kInvalid;
  }

  
  Method method() const
  { return method_; }

  const char* methodString() const
  {
    const char* result = "UNKNOWN";
    switch(method_)
    {
      case kGet:
        result = "GET";
        break;
      case kPost:
        result = "POST";
        break;
      case kHead:
        result = "HEAD";
        break;
      case kPut:
        result = "PUT";
        break;
      case kDelete:
        result = "DELETE";
        break;
      default:
        break;
    }
    return result;
  }

  void setPath(const char* start, const char* end)
  {
    path_.assign(start, end);
  }

  const string& path() const
  { return path_; }

  void setQuery(const char* start, const char* end)
  {
    query_.assign(start, end);
  }

  const string& query() const
  { return query_; }

  void setReceiveTime(Timestamp t)
  { receiveTime_ = t; }

  Timestamp receiveTime() const
  { return receiveTime_; }

// 世界上解析请求头中字段的
  void addHeader(const char* start, const char* colon, const char* end)
  {
    string field(start, colon);
    // 先去头尾空格
    ++colon;
    while (colon < end && isspace(*colon))
    {
      ++colon;
    }
    string value(colon, end);
    while (!value.empty() && isspace(value[value.size()-1]))
    {
      value.resize(value.size()-1);
    }
    // 设置保存
    headers_[field] = value;
  }

  string getHeader(const string& field) const
  {
    string result;
    std::map<string, string>::const_iterator it = headers_.find(field);
    if (it != headers_.end())
    {
      result = it->second;
    }
    return result;
  }

  const std::map<string, string>& headers() const
  { return headers_; }

  void swap(HttpRequest& that)
  {
    std::swap(method_, that.method_);
    std::swap(version_, that.version_);
    path_.swap(that.path_);
    query_.swap(that.query_);
    receiveTime_.swap(that.receiveTime_);
    headers_.swap(that.headers_);
  }

 private:
  Method method_;
  Version version_;
  string path_;
  string query_;
  Timestamp receiveTime_;
  std::map<string, string> headers_;
};

}
}

#endif  // MUDUO_NET_HTTP_HTTPREQUEST_H

 

#ifndefMUDUO_NET_HTTP_HTTPREQUEST_H
#defineMUDUO_NET_HTTP_HTTPREQUEST_H

#include<muduo/base/copyable.h>
#include<muduo/base/Timestamp.h>
#include<muduo/base/Types.h>

#include<map>
#include<assert.h>
#include<stdio.h>

namespacemuduo
{
namespacenet
{

// HttpContext 类只是负责解析的,请求的
// 解析出来的数据是存放在 HttpRequest 类中的
classHttpRequest : publicmuduo::copyable
{
public:
// 解析出来的 请求方法
enum Method
{
kInvalid, kGet, kPost, kHead, kPut, kDelete
};
// http版本
enum Version
{
kUnknown, kHttp10, kHttp11
};

HttpRequest()
: method_(kInvalid),
version_(kUnknowkExpectBodyn)
{
}

// 设置的
voidsetVersion(Version v)
{
version_ = v;
}

Version getVersion() const
{ return version_; }

boolsetMethod(constchar* start, constchar* end)
{
assert(method_ == kInvalid);
string m(start, end);
if (m =="GET")
{
method_ = kGet;
}
elseif (m =="POST")
{
method_ = kPost;
}
elseif (m =="HEAD")
{
method_ = kHead;
}
elseif (m =="PUT")
{
method_ = kPut;
}
elseif (m =="DELETE")
{
method_ = kDelete;
}
else
{
method_ = kInvalid;
}
return method_ != kInvalid;
}

 
Method method() const
{ return method_; }

constchar*methodString() const
{
constchar* result ="UNKNOWN";
switch(method_)
{
case kGet:
result ="GET";
break;
case kPost:
result ="POST";
break;
case kHead:
result ="HEAD";
break;
case kPut:
result ="PUT";
break;
case kDelete:
result ="DELETE";
break;
default:
break;
}
return result;
}

voidsetPath(constchar* start, constchar* end)
{
path_.assign(start, end);
}

const string&path() const
{ return path_; }

voidsetQuery(constchar* start, constchar* end)
{
query_.assign(start, end);
}

const string&query() const
{ return query_; }

voidsetReceiveTime(Timestamp t)
{ receiveTime_ = t; }

Timestamp receiveTime() const
{ return receiveTime_; }

// 世界上解析请求头中字段的
voidaddHeader(constchar* start, constchar* colon, constchar* end)
{
string field(start, colon);
// 先去头尾空格
++colon;
while (colon < end &&isspace(*colon))
{
++colon;
}
string value(colon, end);
while (!value.empty() &&isspace(value[value.size()-1]))
{
value.resize(value.size()-1);
}
// 设置保存
headers_[field] = value;
}

string getHeader(const string& field) const
{
string result;
std::map<string, string>::const_iterator it = headers_.find(field);
if (it != headers_.end())
{
result = it->second;
}
return result;
}

const std::map<string, string>&headers() const
{ return headers_; }

voidswap(HttpRequest& that)
{
std::swap(method_, that.method_);
std::swap(version_, that.version_);
path_.swap(that.path_);
query_.swap(that.query_);
receiveTime_.swap(that.receiveTime_);
headers_.swap(that.headers_);
}

private:
Method method_;
Version version_;
string path_;
string query_;
Timestamp receiveTime_;
std::map<string, string> headers_;
};

}
}

#endif// MUDUO_NET_HTTP_HTTPREQUEST_H

muduo源码-HttpRequest.h

标签:std   ali   assert   const   源码   second   include   san   bool   

原文地址:https://www.cnblogs.com/perfy576/p/8669035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!