1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 * This file contains HTTP parsing functions
26 #ifndef COAP_HTTP_PARSER_H_
27 #define COAP_HTTP_PARSER_H_
35 #include "uarraylist.h"
38 #define CHP_MAX_HF_DATA_LENGTH 1024
39 #define CHP_MAX_HF_NAME_LENGTH 255
40 #define JSON_CONTENT_TYPE "application/json"
41 #define CBOR_CONTENT_TYPE "application/cbor"
42 #define ACCEPT_MEDIA_TYPE (CBOR_CONTENT_TYPE "; q=1.0, " JSON_CONTENT_TYPE "; q=0.5")
45 #define HTTP_OPTION_CACHE_CONTROL "cache-control"
46 #define HTTP_OPTION_ACCEPT "accept"
47 #define HTTP_OPTION_IF_MATCH "if-match"
48 #define HTTP_OPTION_IF_NONE_MATCH "if-none-match"
49 #define HTTP_OPTION_ETAG "etag"
50 #define HTTP_OPTION_CONTENT_TYPE "content-type"
51 #define HTTP_OPTION_CONTENT_LENGTH "content-length"
52 #define HTTP_OPTION_EXPIRES "expires"
55 * @enum HttpResponseResult_t
56 * Enums for HTTP Response values
60 /* Response status code - START HERE */
61 CHP_EMPTY = 0, /**< Empty */
62 CHP_SUCCESS = 200, /**< Success */
63 CHP_CREATED = 201, /**< Created */
64 CHP_ACCEPTED = 202, /**< Accepted */
65 CHP_NO_CONTENT = 204, /**< No Content */
66 CHP_RESET_CONTENT = 205, /**< Reset content */
67 CHP_NOT_MODIFIED = 304, /**< Not Modified */
68 CHP_BAD_REQ = 400, /**< Bad Request */
69 CHP_UNAUTHORIZED_REQ = 401, /**< Unauthorized Request */
70 CHP_FORBIDDEN_REQ = 403, /**< Forbidden Request */
71 CHP_NOT_FOUND = 404, /**< Not found */
72 CHP_NOT_ACCEPTABLE = 406, /**< Not Acceptable */
73 CHP_REQUEST_ENTITY_TOO_LARGE = 413, /**< Request Entity Too Large */
74 CHP_REQUEST_URI_TOO_LARGE = 414, /**< Request URI Too Large */
75 CHP_UNSUPPORTED_MEDIA_TYPE = 415, /**< Unsupported Media type */
76 CHP_INTERNAL_SERVER_ERROR = 500, /**< Internal server Error */
77 CHP_NOT_IMPLEMENTED = 501, /**< Not Implemented */
78 CHP_BAD_GATEWAY = 502, /**< Bad Gateway */
79 CHP_SERVICE_UNAVAILABLE = 503, /**< Service Unavailable */
80 CHP_GATEWAY_TIMEOUT = 504, /**< Gateway Timeout */
81 CHP_VERSION_NOT_SUPPORTED = 505 /**< Version not supported */
82 /* Response status code - END HERE */
83 } HttpResponseResult_t;
86 * Header fields structure to be filled
88 * This structure is used to hold header information.
92 uint16_t optionLength; /**< Option length. **/
93 char optionName[CHP_MAX_HF_NAME_LENGTH]; /**< Option name. **/
94 char optionData[CHP_MAX_HF_DATA_LENGTH]; /**< Option data values. **/
99 CHP_GET = 1, /**< GET */
100 CHP_POST, /**< POST */
102 CHP_DELETE, /**< DELETE */
106 typedef struct HttpRequest_t
108 unsigned short httpMajor;
109 unsigned short httpMinor;
111 u_arraylist_t *headerOptions;
112 char resourceUri[CHP_MAX_HF_DATA_LENGTH];
114 size_t payloadLength;
116 char payloadFormat[CHP_MAX_HF_DATA_LENGTH];
117 char acceptFormat[CHP_MAX_HF_DATA_LENGTH];
120 typedef struct HttpResponse_t
122 unsigned short httpMajor;
123 unsigned short httpMinor;
124 HttpResponseResult_t status;
125 u_arraylist_t *headerOptions;
126 char dataFormat[CHP_MAX_HF_DATA_LENGTH];
128 size_t payloadLength;
131 typedef void (*CHPResponseCallback)(const HttpResponse_t *response, void *context);
134 * Function to initialize Parser and HTTP stack.
136 OCStackResult CHPParserInitialize();
139 * Function to terminate parser and HTTP stack.
141 OCStackResult CHPParserTerminate();
144 * Function to initiate TCP session and post HTTP request. If the method returns
145 * success, payload might be cached by the parser (req->payloadCached) and caller shall not free the
146 * payload if the flag is set.
147 * @param[in] req Object containing HTTP request information.
148 * @param[in] httpcb Callback for http response.
149 * @param[in] context Any app specific context for request
151 OCStackResult CHPPostHttpRequest(HttpRequest_t *req, CHPResponseCallback httpcb,
155 * Macro to verify the validity of input argument.
157 * @param arg log level
158 * @param log_tag log tag
159 * @param log_message log message
160 * @param ret return value
162 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message, ret) \
165 OIC_LOG_V(ERROR, (log_tag), "Invalid input:%s", (log_message)); \
170 * Macro to verify the validity of input argument.
172 * @param arg log level
173 * @param log_tag log tag
174 * @param log_message log message
176 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
177 VERIFY_NON_NULL_RET((arg), (log_tag), (log_message), CA_STATUS_INVALID_PARAM)
180 * Macro to verify the validity of input argument.
182 * @param arg log level
183 * @param log_tag log tag
184 * @param log_message log message
186 #define VERIFY_NON_NULL_VOID(arg, log_tag, log_message) \
187 if (NULL == (arg)) { \
188 OIC_LOG_V(ERROR, (log_tag), "Invalid input:%s", (log_message)); \