http_header.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include "brpc/http_status_code.h" // HTTP_STATUS_*
  18. #include "brpc/http_header.h"
  19. namespace brpc {
  20. HttpHeader::HttpHeader()
  21. : _status_code(HTTP_STATUS_OK)
  22. , _method(HTTP_METHOD_GET)
  23. , _version(1, 1) {
  24. // NOTE: don't forget to clear the field in Clear() as well.
  25. }
  26. void HttpHeader::AppendHeader(const std::string& key,
  27. const butil::StringPiece& value) {
  28. std::string& slot = GetOrAddHeader(key);
  29. if (slot.empty()) {
  30. slot.assign(value.data(), value.size());
  31. } else {
  32. slot.reserve(slot.size() + 1 + value.size());
  33. slot.push_back(',');
  34. slot.append(value.data(), value.size());
  35. }
  36. }
  37. void HttpHeader::Swap(HttpHeader &rhs) {
  38. _headers.swap(rhs._headers);
  39. _uri.Swap(rhs._uri);
  40. std::swap(_status_code, rhs._status_code);
  41. std::swap(_method, rhs._method);
  42. _content_type.swap(rhs._content_type);
  43. _unresolved_path.swap(rhs._unresolved_path);
  44. std::swap(_version, rhs._version);
  45. }
  46. void HttpHeader::Clear() {
  47. _headers.clear();
  48. _uri.Clear();
  49. _status_code = HTTP_STATUS_OK;
  50. _method = HTTP_METHOD_GET;
  51. _content_type.clear();
  52. _unresolved_path.clear();
  53. _version = std::make_pair(1, 1);
  54. }
  55. const char* HttpHeader::reason_phrase() const {
  56. return HttpReasonPhrase(_status_code);
  57. }
  58. void HttpHeader::set_status_code(int status_code) {
  59. _status_code = status_code;
  60. }
  61. const HttpHeader& DefaultHttpHeader() {
  62. static HttpHeader h;
  63. return h;
  64. }
  65. } // namespace brpc