restful_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include "bi-inc/shared_utils.h"
  10. /* Serialization of request and response message
  11. *
  12. * Choices:
  13. * We considered a few options:
  14. * 1. coap
  15. * 2. flatbuffer
  16. * 3. cbor
  17. * 4. attr-containers of our own
  18. * 5. customized serialization for request/response
  19. *
  20. * Now we choose the #5 mainly because we need to quickly get the URL for
  21. * dispatching and sometimes we want to change the URL in the original packet.
  22. * the request format: fixed part: version: (1 byte), code (1 byte), fmt(2
  23. * byte), mid (4 bytes), sender_id(4 bytes), url_len(2 bytes),
  24. * payload_len(4bytes) dynamic part: url (bytes in url_len), payload
  25. *
  26. * response format:
  27. * fixed part: (1 byte), code (1 byte), fmt(2 byte), mid (4 bytes), sender_id(4
  28. * bytes), payload_len(4bytes) dynamic part: payload
  29. */
  30. #define REQUES_PACKET_VER 1
  31. #define REQUEST_PACKET_FIX_PART_LEN 18
  32. #define REQUEST_PACKET_URL_OFFSET REQUEST_PACKET_FIX_PART_LEN
  33. #define REQUEST_PACKET_URL_LEN \
  34. *((uint16 *)((char *)buffer + 12)) /* to ensure little endian */
  35. #define REQUEST_PACKET_PAYLOAD_LEN \
  36. *((uint32 *)((char *)buffer + 14)) /* to ensure little endian */
  37. #define REQUEST_PACKET_URL(buffer) ((char *)buffer + REQUEST_PACKET_URL_OFFSET)
  38. #define REQUEST_PACKET_PAYLOAD(buffer) \
  39. ((char *)buffer + REQUEST_PACKET_URL_OFFSET \
  40. + REQUEST_PACKET_URL_LEN(buffer))
  41. #define RESPONSE_PACKET_FIX_PART_LEN 16
  42. char *
  43. pack_request(request_t *request, int *size)
  44. {
  45. int url_len = strlen(request->url) + 1;
  46. int len = REQUEST_PACKET_FIX_PART_LEN + url_len + request->payload_len;
  47. uint16 u16;
  48. uint32 u32;
  49. char *packet;
  50. if ((packet = (char *)WA_MALLOC(len)) == NULL)
  51. return NULL;
  52. /* TODO: ensure little endian for words and dwords */
  53. *packet = REQUES_PACKET_VER;
  54. *((uint8 *)(packet + 1)) = request->action;
  55. u16 = htons(request->fmt);
  56. memcpy(packet + 2, &u16, 2);
  57. u32 = htonl(request->mid);
  58. memcpy(packet + 4, &u32, 4);
  59. u32 = htonl(request->sender);
  60. memcpy(packet + 8, &u32, 4);
  61. u16 = htons(url_len);
  62. memcpy(packet + 12, &u16, 2);
  63. u32 = htonl(request->payload_len);
  64. memcpy(packet + 14, &u32, 4);
  65. strcpy(packet + REQUEST_PACKET_URL_OFFSET, request->url);
  66. memcpy(packet + REQUEST_PACKET_URL_OFFSET + url_len, request->payload,
  67. request->payload_len);
  68. *size = len;
  69. return packet;
  70. }
  71. void
  72. free_req_resp_packet(char *packet)
  73. {
  74. WA_FREE(packet);
  75. }
  76. request_t *
  77. unpack_request(char *packet, int size, request_t *request)
  78. {
  79. uint16 url_len, u16;
  80. uint32 payload_len, u32;
  81. if (*packet != REQUES_PACKET_VER) {
  82. return NULL;
  83. }
  84. if (size < REQUEST_PACKET_FIX_PART_LEN) {
  85. return NULL;
  86. }
  87. memcpy(&u16, packet + 12, 2);
  88. url_len = ntohs(u16);
  89. memcpy(&u32, packet + 14, 4);
  90. payload_len = ntohl(u32);
  91. if (size != (REQUEST_PACKET_FIX_PART_LEN + url_len + payload_len)) {
  92. return NULL;
  93. }
  94. if (*(packet + REQUEST_PACKET_FIX_PART_LEN + url_len - 1) != 0) {
  95. return NULL;
  96. }
  97. request->action = *((uint8 *)(packet + 1));
  98. memcpy(&u16, packet + 2, 2);
  99. request->fmt = ntohs(u16);
  100. memcpy(&u32, packet + 4, 4);
  101. request->mid = ntohl(u32);
  102. memcpy(&u32, packet + 8, 4);
  103. request->sender = ntohl(u32);
  104. request->payload_len = payload_len;
  105. request->url = REQUEST_PACKET_URL(packet);
  106. if (payload_len > 0)
  107. request->payload = packet + REQUEST_PACKET_URL_OFFSET + url_len;
  108. else
  109. request->payload = NULL;
  110. return request;
  111. }
  112. char *
  113. pack_response(response_t *response, int *size)
  114. {
  115. int len = RESPONSE_PACKET_FIX_PART_LEN + response->payload_len;
  116. uint16 u16;
  117. uint32 u32;
  118. char *packet;
  119. if ((packet = (char *)WA_MALLOC(len)) == NULL)
  120. return NULL;
  121. /* TODO: ensure little endian for words and dwords */
  122. *packet = REQUES_PACKET_VER;
  123. *((uint8 *)(packet + 1)) = response->status;
  124. u16 = htons(response->fmt);
  125. memcpy(packet + 2, &u16, 2);
  126. u32 = htonl(response->mid);
  127. memcpy(packet + 4, &u32, 4);
  128. u32 = htonl(response->reciever);
  129. memcpy(packet + 8, &u32, 4);
  130. u32 = htonl(response->payload_len);
  131. memcpy(packet + 12, &u32, 4);
  132. memcpy(packet + RESPONSE_PACKET_FIX_PART_LEN, response->payload,
  133. response->payload_len);
  134. *size = len;
  135. return packet;
  136. }
  137. response_t *
  138. unpack_response(char *packet, int size, response_t *response)
  139. {
  140. uint16 u16;
  141. uint32 payload_len, u32;
  142. if (*packet != REQUES_PACKET_VER)
  143. return NULL;
  144. if (size < RESPONSE_PACKET_FIX_PART_LEN)
  145. return NULL;
  146. memcpy(&u32, packet + 12, 4);
  147. payload_len = ntohl(u32);
  148. if (size != (RESPONSE_PACKET_FIX_PART_LEN + payload_len))
  149. return NULL;
  150. response->status = *((uint8 *)(packet + 1));
  151. memcpy(&u16, packet + 2, 2);
  152. response->fmt = ntohs(u16);
  153. memcpy(&u32, packet + 4, 4);
  154. response->mid = ntohl(u32);
  155. memcpy(&u32, packet + 8, 4);
  156. response->reciever = ntohl(u32);
  157. response->payload_len = payload_len;
  158. if (payload_len > 0)
  159. response->payload = packet + RESPONSE_PACKET_FIX_PART_LEN;
  160. else
  161. response->payload = NULL;
  162. return response;
  163. }
  164. request_t *
  165. clone_request(request_t *request)
  166. {
  167. /* deep clone */
  168. request_t *req = (request_t *)WA_MALLOC(sizeof(request_t));
  169. if (req == NULL)
  170. return NULL;
  171. memset(req, 0, sizeof(*req));
  172. req->action = request->action;
  173. req->fmt = request->fmt;
  174. req->url = wa_strdup(request->url);
  175. req->sender = request->sender;
  176. req->mid = request->mid;
  177. if (req->url == NULL)
  178. goto fail;
  179. req->payload_len = request->payload_len;
  180. if (request->payload_len) {
  181. req->payload = (char *)WA_MALLOC(request->payload_len);
  182. if (!req->payload)
  183. goto fail;
  184. memcpy(req->payload, request->payload, request->payload_len);
  185. }
  186. else {
  187. /* when payload_len is 0, the payload may be used for
  188. carrying some handle or integer */
  189. req->payload = request->payload;
  190. }
  191. return req;
  192. fail:
  193. request_cleaner(req);
  194. return NULL;
  195. }
  196. void
  197. request_cleaner(request_t *request)
  198. {
  199. if (request->url != NULL)
  200. WA_FREE(request->url);
  201. if (request->payload != NULL && request->payload_len > 0)
  202. WA_FREE(request->payload);
  203. WA_FREE(request);
  204. }
  205. void
  206. response_cleaner(response_t *response)
  207. {
  208. if (response->payload != NULL && response->payload_len > 0)
  209. WA_FREE(response->payload);
  210. WA_FREE(response);
  211. }
  212. response_t *
  213. clone_response(response_t *response)
  214. {
  215. response_t *clone = (response_t *)WA_MALLOC(sizeof(response_t));
  216. if (clone == NULL)
  217. return NULL;
  218. memset(clone, 0, sizeof(*clone));
  219. clone->fmt = response->fmt;
  220. clone->mid = response->mid;
  221. clone->status = response->status;
  222. clone->reciever = response->reciever;
  223. clone->payload_len = response->payload_len;
  224. if (clone->payload_len) {
  225. clone->payload = (char *)WA_MALLOC(response->payload_len);
  226. if (!clone->payload)
  227. goto fail;
  228. memcpy(clone->payload, response->payload, response->payload_len);
  229. }
  230. else {
  231. /* when payload_len is 0, the payload may be used for
  232. carrying some handle or integer */
  233. clone->payload = response->payload;
  234. }
  235. return clone;
  236. fail:
  237. response_cleaner(clone);
  238. return NULL;
  239. }
  240. response_t *
  241. set_response(response_t *response, int status, int fmt, const char *payload,
  242. int payload_len)
  243. {
  244. response->payload = (void *)payload;
  245. response->payload_len = payload_len;
  246. response->status = status;
  247. response->fmt = fmt;
  248. return response;
  249. }
  250. response_t *
  251. make_response_for_request(request_t *request, response_t *response)
  252. {
  253. response->mid = request->mid;
  254. response->reciever = request->sender;
  255. return response;
  256. }
  257. static unsigned int mid = 0;
  258. request_t *
  259. init_request(request_t *request, char *url, int action, int fmt, void *payload,
  260. int payload_len)
  261. {
  262. request->url = url;
  263. request->action = action;
  264. request->fmt = fmt;
  265. request->payload = payload;
  266. request->payload_len = payload_len;
  267. request->mid = ++mid;
  268. return request;
  269. }
  270. /*
  271. check if the "url" is starting with "leading_str"
  272. return: 0 - not match; >0 - the offset of matched url, include any "/" at the
  273. end notes:
  274. 1. it ensures the leading_str "/abc" can pass "/abc/cde" and "/abc/, but fail
  275. "/ab" and "/abcd". leading_str "/abc/" can pass "/abc"
  276. 2. it omit the '/' at the first char
  277. 3. it ensure the leading_str "/abc" can pass "/abc?cde
  278. */
  279. int
  280. check_url_start(const char *url, int url_len, const char *leading_str)
  281. {
  282. int offset = 0;
  283. if (*leading_str == '/')
  284. leading_str++;
  285. if (url_len > 0 && *url == '/') {
  286. url_len--;
  287. url++;
  288. offset++;
  289. }
  290. int len = strlen(leading_str);
  291. if (len == 0)
  292. return 0;
  293. /* ensure leading_str not end with "/" */
  294. if (leading_str[len - 1] == '/') {
  295. len--;
  296. if (len == 0)
  297. return 0;
  298. }
  299. /* equal length */
  300. if (url_len == len) {
  301. if (memcmp(url, leading_str, url_len) == 0) {
  302. return (offset + len);
  303. }
  304. else {
  305. return 0;
  306. }
  307. }
  308. if (url_len < len)
  309. return 0;
  310. else if (memcmp(url, leading_str, len) != 0)
  311. return 0;
  312. else if (url[len] != '/' && url[len] != '?')
  313. return 0;
  314. else
  315. return (offset + len + 1);
  316. }
  317. // * @pattern:
  318. // * sample 1: /abcd, match /abcd only
  319. // * sample 2: /abcd/ match match "/abcd" and "/abcd/*"
  320. // * sample 3: /abcd*, match any url started with "/abcd"
  321. // * sample 4: /abcd/*, exclude "/abcd"
  322. bool
  323. match_url(char *pattern, char *matched)
  324. {
  325. if (*pattern == '/')
  326. pattern++;
  327. if (*matched == '/')
  328. matched++;
  329. int matched_len = strlen(matched);
  330. if (matched_len == 0)
  331. return false;
  332. if (matched[matched_len - 1] == '/') {
  333. matched_len--;
  334. if (matched_len == 0)
  335. return false;
  336. }
  337. int len = strlen(pattern);
  338. if (len == 0)
  339. return false;
  340. if (pattern[len - 1] == '/') {
  341. len--;
  342. if (strncmp(pattern, matched, len) != 0)
  343. return false;
  344. if (len == matched_len)
  345. return true;
  346. if (matched_len > len && matched[len] == '/')
  347. return true;
  348. return false;
  349. }
  350. else if (pattern[len - 1] == '*') {
  351. if (pattern[len - 2] == '/') {
  352. if (strncmp(pattern, matched, len - 1) == 0)
  353. return true;
  354. else
  355. return false;
  356. }
  357. else {
  358. return (strncmp(pattern, matched, len - 1) == 0);
  359. }
  360. }
  361. else {
  362. return (strcmp(pattern, matched) == 0);
  363. }
  364. }
  365. /*
  366. * get the value of the key from following format buffer:
  367. * key1=value1;key2=value2;key3=value3
  368. */
  369. char *
  370. find_key_value(char *buffer, int buffer_len, char *key, char *value,
  371. int value_len, char delimiter)
  372. {
  373. char *p = buffer;
  374. int remaining = buffer_len;
  375. int key_len = strlen(key);
  376. while (*p != 0 && remaining > 0) {
  377. while (*p == ' ' || *p == delimiter) {
  378. p++;
  379. remaining--;
  380. }
  381. if (remaining <= key_len)
  382. return NULL;
  383. /* find the key */
  384. if (0 == strncmp(p, key, key_len) && p[key_len] == '=') {
  385. p += (key_len + 1);
  386. remaining -= (key_len + 1);
  387. char *v = value;
  388. memset(value, 0, value_len);
  389. value_len--; /* ensure last char is 0 */
  390. while (*p != delimiter && remaining > 0 && value_len > 0) {
  391. *v++ = *p++;
  392. remaining--;
  393. value_len--;
  394. }
  395. return value;
  396. }
  397. /* goto next key */
  398. while (*p != delimiter && remaining > 0) {
  399. p++;
  400. remaining--;
  401. }
  402. }
  403. return NULL;
  404. }