servlet.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "yql_servlet.h"
  2. namespace NYql {
  3. namespace NHttp {
  4. ///////////////////////////////////////////////////////////////////////////////
  5. // TResponse
  6. ///////////////////////////////////////////////////////////////////////////////
  7. void TResponse::OutTo(IOutputStream& out) const {
  8. TVector<IOutputStream::TPart> parts;
  9. const size_t FIRST_LINE_PARTS = 3;
  10. const size_t HEADERS_PARTS = Headers.Count() * 4;
  11. const size_t CONTENT_PARTS = 5;
  12. parts.reserve(FIRST_LINE_PARTS + HEADERS_PARTS + CONTENT_PARTS);
  13. // first line
  14. parts.push_back(IOutputStream::TPart(TStringBuf("HTTP/1.1 ")));
  15. parts.push_back(IOutputStream::TPart(HttpCodeStrEx(Code)));
  16. parts.push_back(IOutputStream::TPart::CrLf());
  17. // headers
  18. for (THttpHeaders::TConstIterator i = Headers.Begin(); i != Headers.End(); ++i) {
  19. parts.push_back(IOutputStream::TPart(i->Name()));
  20. parts.push_back(IOutputStream::TPart(TStringBuf(": ")));
  21. parts.push_back(IOutputStream::TPart(i->Value()));
  22. parts.push_back(IOutputStream::TPart::CrLf());
  23. }
  24. char buf[50];
  25. if (!Body.Empty()) {
  26. TMemoryOutput mo(buf, sizeof(buf));
  27. mo << Body.Size();
  28. parts.push_back(IOutputStream::TPart(TStringBuf("Content-Length: ")));
  29. parts.push_back(IOutputStream::TPart(buf, mo.Buf() - buf));
  30. parts.push_back(IOutputStream::TPart::CrLf());
  31. }
  32. // body
  33. parts.push_back(IOutputStream::TPart::CrLf());
  34. if (!Body.Empty()) {
  35. parts.push_back(IOutputStream::TPart(Body.AsCharPtr(), Body.Size()));
  36. }
  37. out.Write(parts.data(), parts.size());
  38. }
  39. } // namspace NNttp
  40. } // namspace NYql