rpc_message_thrift.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Copyright (c) 2020 sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <errno.h>
  14. #include <workflow/HttpUtil.h>
  15. #include "rpc_message_thrift.h"
  16. namespace srpc
  17. {
  18. static int thrift_parser_append_message(const void *buf, size_t *size,
  19. ThriftBuffer *TBuffer)
  20. {
  21. if (TBuffer->status == THRIFT_PARSE_END)
  22. {
  23. *size = 0;
  24. return 1;
  25. }
  26. /*
  27. if (TBuffer->status == THRIFT_PARSE_INIT)
  28. TBuffer->status = THRIFT_GET_FRAME_SIZE;
  29. */
  30. if (TBuffer->status == THRIFT_GET_FRAME_SIZE)
  31. {
  32. size_t framesize_bytelen = sizeof (TBuffer->framesize);
  33. char *readbuf = (char*)&TBuffer->framesize;
  34. size_t read_size = 0;
  35. for (size_t i = 0; i < *size; i++)
  36. {
  37. read_size++;
  38. ((char *)readbuf)[TBuffer->framesize_read_byte] = ((char *)buf)[i];
  39. if (++TBuffer->framesize_read_byte == framesize_bytelen)
  40. {
  41. TBuffer->status = THRIFT_GET_DATA;
  42. TBuffer->framesize = ntohl(TBuffer->framesize);
  43. if (TBuffer->framesize < 0)
  44. {
  45. errno = EBADMSG;
  46. return -1;
  47. }
  48. //TBuffer->readbuf = new char[TBuffer->framesize];
  49. break;
  50. }
  51. }
  52. size_t left_size = *size - read_size;
  53. *size = read_size;
  54. if (left_size == 0)
  55. {
  56. if (TBuffer->status == THRIFT_GET_DATA && TBuffer->framesize == 0)
  57. {
  58. TBuffer->status = THRIFT_PARSE_END;
  59. return 1;
  60. }
  61. else
  62. return 0;
  63. }
  64. else
  65. {
  66. int ret = thrift_parser_append_message((char *)buf + read_size,
  67. &left_size, TBuffer);
  68. *size += left_size;
  69. return ret;
  70. }
  71. }
  72. else if (TBuffer->status == THRIFT_GET_DATA)
  73. {
  74. size_t read_size = *size;
  75. if (TBuffer->readbuf_size + *size > (size_t)TBuffer->framesize)
  76. read_size = TBuffer->framesize - TBuffer->readbuf_size;
  77. TBuffer->buffer->append((const char *)buf, read_size, BUFFER_MODE_COPY);
  78. TBuffer->readbuf_size += read_size;
  79. *size = read_size;
  80. if (TBuffer->readbuf_size < (size_t)TBuffer->framesize)
  81. return 0;
  82. else if (TBuffer->readbuf_size == (uint32_t)TBuffer->framesize)
  83. {
  84. TBuffer->status = THRIFT_PARSE_END;
  85. return 1;
  86. }
  87. }
  88. errno = EBADMSG;
  89. return -1;
  90. }
  91. int ThriftMessage::append(const void *buf, size_t *size, size_t size_limit)
  92. {
  93. return thrift_parser_append_message(buf, size, &TBuffer_);
  94. }
  95. bool ThriftResponse::serialize_meta()
  96. {
  97. if (status_code_ == RPCStatusOK)
  98. TBuffer_.meta.message_type = TMT_REPLY;
  99. else
  100. {
  101. ThriftException ex;
  102. ex.type = (status_code_ == RPCStatusMethodNotFound ?
  103. TET_UNKNOWN_METHOD :
  104. TET_UNKNOWN);
  105. ex.message = errmsg_;
  106. ex.descriptor->writer(&ex, &TBuffer_);
  107. TBuffer_.meta.message_type = TMT_EXCEPTION;
  108. }
  109. return TBuffer_.writeMessageBegin();
  110. }
  111. const char *thrift_error2errmsg(int error)
  112. {
  113. switch (error)
  114. {
  115. case TET_UNKNOWN:
  116. return "TApplicationException: Unknown application exception";
  117. case TET_UNKNOWN_METHOD:
  118. return "TApplicationException: Unknown method";
  119. case TET_INVALID_MESSAGE_TYPE:
  120. return "TApplicationException: Invalid message type";
  121. case TET_WRONG_METHOD_NAME:
  122. return "TApplicationException: Wrong method name";
  123. case TET_BAD_SEQUENCE_ID:
  124. return "TApplicationException: Bad sequence identifier";
  125. case TET_MISSING_RESULT:
  126. return "TApplicationException: Missing result";
  127. case TET_INTERNAL_ERROR:
  128. return "TApplicationException: Internal error";
  129. case TET_PROTOCOL_ERROR:
  130. return "TApplicationException: Protocol error";
  131. case TET_INVALID_TRANSFORM:
  132. return "TApplicationException: Invalid transform";
  133. case TET_INVALID_PROTOCOL:
  134. return "TApplicationException: Invalid protocol";
  135. case TET_UNSUPPORTED_CLIENT_TYPE:
  136. return "TApplicationException: Unsupported client type";
  137. default:
  138. return "TApplicationException: (Invalid exception type)";
  139. };
  140. }
  141. bool ThriftResponse::deserialize_meta()
  142. {
  143. if (TBuffer_.readMessageBegin())
  144. {
  145. if (TBuffer_.meta.message_type == TMT_EXCEPTION)
  146. {
  147. ThriftException ex;
  148. if (ex.descriptor->reader(&TBuffer_, &ex))
  149. {
  150. status_code_ = (ex.type == TET_UNKNOWN_METHOD ?
  151. RPCStatusMethodNotFound :
  152. RPCStatusMetaError);
  153. error_ = ex.type;
  154. errmsg_ = ex.message;
  155. }
  156. else
  157. {
  158. status_code_ = RPCStatusMetaError;
  159. error_ = TET_INTERNAL_ERROR;
  160. errmsg_ = thrift_error2errmsg(error_);
  161. }
  162. }
  163. return true;
  164. }
  165. return false;
  166. }
  167. const char *ThriftResponse::get_errmsg() const
  168. {
  169. if (!errmsg_.empty())
  170. return errmsg_.c_str();
  171. return thrift_error2errmsg(error_);
  172. }
  173. bool ThriftHttpRequest::serialize_meta()
  174. {
  175. if (buf_.size() > 0x7FFFFFFF)
  176. return false;
  177. if (!this->ThriftRequest::serialize_meta())
  178. return false;
  179. set_http_version("HTTP/1.1");
  180. set_method("POST");
  181. set_request_uri("/");
  182. set_header_pair("Connection", "Keep-Alive");
  183. set_header_pair("Content-Type", "application/x-thrift");
  184. set_header_pair("Content-Length",
  185. std::to_string(TBuffer_.meta.writebuf.size() + buf_.size()));
  186. this->append_output_body_nocopy(TBuffer_.meta.writebuf.c_str(),
  187. TBuffer_.meta.writebuf.size());
  188. const void *buf;
  189. size_t buflen;
  190. while (buflen = buf_.fetch(&buf), buf && buflen > 0)
  191. this->append_output_body_nocopy(buf, buflen);
  192. return true;
  193. }
  194. bool ThriftHttpRequest::deserialize_meta()
  195. {
  196. const void *body;
  197. size_t body_len;
  198. get_parsed_body(&body, &body_len);
  199. if (body_len > 0x7FFFFFFF)
  200. return false;
  201. buf_.append((const char *)body, body_len, BUFFER_MODE_NOCOPY);
  202. TBuffer_.framesize = (int32_t)body_len;
  203. return this->ThriftRequest::deserialize_meta();
  204. }
  205. bool ThriftHttpResponse::serialize_meta()
  206. {
  207. if (buf_.size() > 0x7FFFFFFF)
  208. return false;
  209. if (!this->ThriftResponse::serialize_meta())
  210. return false;
  211. int rpc_status_code = this->get_status_code();
  212. const char *http_status_code = this->protocol::HttpResponse::get_status_code();
  213. set_http_version("HTTP/1.1");
  214. if (rpc_status_code == RPCStatusOK)
  215. {
  216. if (http_status_code)
  217. protocol::HttpUtil::set_response_status(this, atoi(http_status_code));
  218. else
  219. protocol::HttpUtil::set_response_status(this, HttpStatusOK);
  220. }
  221. else if (rpc_status_code == RPCStatusServiceNotFound
  222. || rpc_status_code == RPCStatusMethodNotFound
  223. || rpc_status_code == RPCStatusMetaError
  224. || rpc_status_code == RPCStatusURIInvalid)
  225. protocol::HttpUtil::set_response_status(this, HttpStatusBadRequest);
  226. else if (rpc_status_code == RPCStatusRespCompressNotSupported
  227. || rpc_status_code == RPCStatusRespDecompressNotSupported
  228. || rpc_status_code == RPCStatusIDLSerializeNotSupported
  229. || rpc_status_code == RPCStatusIDLDeserializeNotSupported)
  230. protocol::HttpUtil::set_response_status(this, HttpStatusNotImplemented);
  231. else if (rpc_status_code == RPCStatusUpstreamFailed)
  232. protocol::HttpUtil::set_response_status(this, HttpStatusServiceUnavailable);
  233. else
  234. protocol::HttpUtil::set_response_status(this, HttpStatusInternalServerError);
  235. set_header_pair("Connection", "Keep-Alive");
  236. set_header_pair("Content-Type", "application/x-thrift");
  237. set_header_pair("Content-Length",
  238. std::to_string(TBuffer_.meta.writebuf.size() + buf_.size()));
  239. this->append_output_body_nocopy(TBuffer_.meta.writebuf.c_str(),
  240. TBuffer_.meta.writebuf.size());
  241. const void *buf;
  242. size_t buflen;
  243. while (buflen = buf_.fetch(&buf), buf && buflen > 0)
  244. this->append_output_body_nocopy(buf, buflen);
  245. return true;
  246. }
  247. bool ThriftHttpResponse::deserialize_meta()
  248. {
  249. const void *body;
  250. size_t body_len;
  251. get_parsed_body(&body, &body_len);
  252. if (body_len > 0x7FFFFFFF)
  253. return false;
  254. buf_.append((const char *)body, body_len, BUFFER_MODE_NOCOPY);
  255. TBuffer_.framesize = (int32_t)body_len;
  256. return this->ThriftResponse::deserialize_meta();
  257. }
  258. bool ThriftHttpRequest::set_http_header(const std::string& name,
  259. const std::string& value)
  260. {
  261. return this->protocol::HttpMessage::set_header_pair(name, value);
  262. }
  263. bool ThriftHttpRequest::add_http_header(const std::string& name,
  264. const std::string& value)
  265. {
  266. return this->protocol::HttpMessage::add_header_pair(name, value);
  267. }
  268. bool ThriftHttpRequest::get_http_header(const std::string& name,
  269. std::string& value) const
  270. {
  271. protocol::HttpHeaderCursor cursor(this);
  272. return cursor.find(name, value);
  273. }
  274. bool ThriftHttpResponse::set_http_header(const std::string& name,
  275. const std::string& value)
  276. {
  277. return this->protocol::HttpMessage::set_header_pair(name, value);
  278. }
  279. bool ThriftHttpResponse::add_http_header(const std::string& name,
  280. const std::string& value)
  281. {
  282. return this->protocol::HttpMessage::add_header_pair(name, value);
  283. }
  284. bool ThriftHttpResponse::get_http_header(const std::string& name,
  285. std::string& value) const
  286. {
  287. protocol::HttpHeaderCursor cursor(this);
  288. return cursor.find(name, value);
  289. }
  290. } // namespace srpc