Commit 134247da authored by Paul Asmuth's avatar Paul Asmuth
Browse files

read request body

parent 90477b57
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -25,6 +25,10 @@ HTTPInputStream::HTTPInputStream(
HTTPInputStream::~HTTPInputStream() {
}

util::InputStream* HTTPInputStream::getInputStream() const {
  return input_;
}

void HTTPInputStream::readStatusLine(
    std::string* method,
    std::string* url,
+2 −0
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ public:
  void readHeaders(
      std::vector<std::pair<std::string, std::string>>* target);

  util::InputStream* getInputStream() const;

protected:
  void readNextByte(std::string* target);
  util::InputStream* input_;
+9 −0
Original line number Diff line number Diff line
@@ -42,5 +42,14 @@ void HTTPMessage::addHeader(const std::string& key, const std::string& value) {
  headers_.emplace_back(key, value);
}

const std::string& HTTPMessage::getBody() const {
  return body_;
}

void HTTPMessage::addBody(const std::string& body) {
  body_ = body;
  addHeader("Content-Length", std::to_string(body_.size()));
}

}
}
+4 −0
Original line number Diff line number Diff line
@@ -25,10 +25,14 @@ public:
  const std::string& getHeader(const std::string& key) const;
  void addHeader(const std::string& key, const std::string& value);

  const std::string& getBody() const;
  void addBody(const std::string& body);

protected:
  std::string version_;
  static std::string kEmptyHeader;
  std::vector<std::pair<std::string, std::string>> headers_;
  std::string body_;
};

}
+14 −0
Original line number Diff line number Diff line
@@ -16,6 +16,20 @@ namespace http {
void HTTPRequest::readFromInputStream(HTTPInputStream* input) {
  input->readStatusLine(&method_, &url_, &version_);
  input->readHeaders(&headers_);

  const auto& content_length_header = getHeader("Content-Length");
  int content_length = 0;
  if (content_length_header.size() > 0) {
    try {
      content_length = std::stoi(content_length_header);
    } catch (std::exception e){
      /* invalid content length */
    }
  }

  if (content_length > 0) {
    input->getInputStream()->readNextBytes(&body_, content_length);
  }
}

const std::string& HTTPRequest::getMethod() const {
Loading