Commit bb424378 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

httpoutputstream

parent 592f37fd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -9,7 +9,9 @@ set(FNORDMETRIC_SOURCES
    stage/src/fnordmetric/ev/acceptor.cc
    stage/src/fnordmetric/ev/eventloop.cc
    stage/src/fnordmetric/http/httpinputstream.cc
    stage/src/fnordmetric/http/httpoutputstream.cc
    stage/src/fnordmetric/http/httprequest.cc
    stage/src/fnordmetric/http/httpresponse.cc
    stage/src/fnordmetric/http/httpserver.cc
    stage/src/fnordmetric/util/exceptionhandler.cc
    stage/src/fnordmetric/util/format.cc
+39 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnordmetric/http/httpoutputstream.h>

namespace fnordmetric {
namespace http {

HTTPOutputStream::HTTPOutputStream(
    util::OutputStream* output_stream) :
    output_(output_stream) {}

void HTTPOutputStream::writeStatusLine(
    const std::string& version,
    int status) {
  output_->printf("%s %i %s\r\n", version.c_str(), status, "OK"); // FIXPAUL
}

void HTTPOutputStream::writeHeaders(
    const std::vector<std::pair<std::string, std::string>>& headers) {
  for (const auto& header : headers) {
    output_->printf("%s: %s\r\n", header.first.c_str(), header.second.c_str());
  }

  output_->printf("\r\n");
}

util::OutputStream* HTTPOutputStream::getOutputStream() const {
  return output_;
}

}
}
+43 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef __FNORDMETRIC_HTTPOUTPUTSTREAM_H
#define __FNORDMETRIC_HTTPOUTPUTSTREAM_H
#include <vector>
#include <string>
#include <utility>
#include <fnordmetric/util/outputstream.h>

namespace fnordmetric {
namespace http {

class HTTPOutputStream {
public:

  /**
   * @param output_stream the output stream -- does not transfer ownership
   */
  HTTPOutputStream(util::OutputStream* output_stream);

  void writeStatusLine(
      const std::string& version,
      int status);

  void writeHeaders(
      const std::vector<std::pair<std::string, std::string>>& headers);

  util::OutputStream* getOutputStream() const;

protected:
  util::OutputStream* output_;
};

}
}
#endif
+40 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2011-2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnordmetric/http/httpresponse.h>
#include <fnordmetric/http/httpoutputstream.h>

namespace fnordmetric {
namespace http {

HTTPResponse::HTTPResponse() :
    version_("HTTP/1.1"),
    status_(400) {}

void HTTPResponse::setStatus(int status) {
  status_ = status;
}

void HTTPResponse::addHeader(const std::string& key, const std::string& value) {
  headers_.emplace_back(key, value);
}

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

void HTTPResponse::writeToOutputStream(HTTPOutputStream* output) {
  output->writeStatusLine(version_, status_);
  output->writeHeaders(headers_);
  output->getOutputStream()->write(body_);
}

}
}
+14 −0
Original line number Diff line number Diff line
@@ -15,9 +15,23 @@

namespace fnordmetric {
namespace http {
class HTTPOutputStream;

class HTTPResponse {
public:
  HTTPResponse();

  void setStatus(int status);
  void addHeader(const std::string& key, const std::string& value);
  void addBody(const std::string& body);

  void writeToOutputStream(HTTPOutputStream* output);

protected:
  std::string version_;
  int status_;
  std::vector<std::pair<std::string, std::string>> headers_;
  std::string body_;
};

}
Loading