Skip to content

Commit

Permalink
Make CSGI::Response a bit less awkward
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeusz Sośnierz committed Apr 9, 2012
1 parent 61d9109 commit 730956d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
13 changes: 7 additions & 6 deletions app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ CSGI::Response app(CSGI::Env& env)
std::cerr << it->first << " => " << it->second << std::endl;
}

std::string body = "Hello, world!";
resp.content = "Hello, world!";

std::stringstream len;
len << body.length();
resp.first = 200;
resp.second.first["Content-Type"] = "text/plain";
resp.second.first["Content-Length"] = len.str().c_str();
resp.second.second = body;
len << resp.content.length();

resp.status = 200;
resp.headers["Content-Type"] = "text/plain";
resp.headers["Content-Length"] = len.str().c_str();

return resp;
}
Expand Down
8 changes: 4 additions & 4 deletions csgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ CSGI::Env CSGI::Server::parse_request(int fd)

void CSGI::Server::send_response(Response& resp, int fd) {
std::stringstream out;
out << "HTTP/1.1 " << resp.first << " OK\r\n";
out << "HTTP/1.1 " << resp.status << " OK\r\n";
Headers::iterator it;
for (it = resp.second.first.begin();
it != resp.second.first.end();
for (it = resp.headers.begin();
it != resp.headers.end();
it++) {
out << it->first << ": " << it->second << "\r\n";
}
out << "\r\n";
out << resp.second.second << "\r\n";
out << resp.content << "\r\n";
send(fd, out.str().c_str(), out.str().length(), 0);
}
6 changes: 5 additions & 1 deletion csgi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ typedef int Status;

typedef std::pair<Headers, Body> Content;

typedef std::pair<Status, Content> Response;
struct Response {
Status status;
Headers headers;
Body content;
};

typedef Response(*Application)(Env&);

Expand Down

0 comments on commit 730956d

Please sign in to comment.