Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / coap-http-proxy / include / CoapHttpParser.h
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains HTTP parsing functions
24  */
25
26 #ifndef COAP_HTTP_PARSER_H_
27 #define COAP_HTTP_PARSER_H_
28
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #endif
33
34 #include <unistd.h>
35 #include "uarraylist.h"
36 #include "octypes.h"
37
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")
43
44 // HTTP Option types
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"
53
54 /**
55  * @enum HttpResponseResult_t
56  * Enums for HTTP Response values
57  */
58 typedef enum
59 {
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;
84
85 /**
86  * Header fields structure to be filled
87  *
88  * This structure is used to hold header information.
89  */
90 typedef struct
91 {
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. **/
95 } HttpHeaderOption_t;
96
97 typedef enum
98 {
99     CHP_GET = 1,   /**< GET */
100     CHP_POST,      /**< POST */
101     CHP_PUT,       /**< PUT */
102     CHP_DELETE,    /**< DELETE */
103     CHP_INVALID
104 }HttpMethod_t;
105
106 typedef struct HttpRequest_t
107 {
108     unsigned short httpMajor;
109     unsigned short httpMinor;
110     HttpMethod_t method;
111     u_arraylist_t *headerOptions;
112     char resourceUri[CHP_MAX_HF_DATA_LENGTH];
113     void *payload;
114     size_t payloadLength;
115     bool payloadCached;
116     char payloadFormat[CHP_MAX_HF_DATA_LENGTH];
117     char acceptFormat[CHP_MAX_HF_DATA_LENGTH];
118 }HttpRequest_t;
119
120 typedef struct HttpResponse_t
121 {
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];
127     void *payload;
128     size_t payloadLength;
129 }HttpResponse_t;
130
131 typedef void (*CHPResponseCallback)(const HttpResponse_t *response, void *context);
132
133 /**
134  * Function to initialize Parser and HTTP stack.
135  */
136 OCStackResult CHPParserInitialize();
137
138 /**
139  * Function to terminate parser and HTTP stack.
140  */
141 OCStackResult CHPParserTerminate();
142
143 /**
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
150  */
151 OCStackResult CHPPostHttpRequest(HttpRequest_t *req, CHPResponseCallback httpcb,
152                                  void *context);
153
154 /**
155  * Macro to verify the validity of input argument.
156  *
157  * @param  arg  log level
158  * @param  log_tag  log tag
159  * @param  log_message  log message
160  * @param  ret  return value
161  */
162 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message, ret) \
163     if (NULL == (arg)) \
164     { \
165         OIC_LOG_V(ERROR, (log_tag), "Invalid input:%s", (log_message)); \
166         return (ret); \
167     } \
168
169 /**
170  * Macro to verify the validity of input argument.
171  *
172  * @param  arg  log level
173  * @param  log_tag  log tag
174  * @param  log_message  log message
175  */
176 #define VERIFY_NON_NULL(arg, log_tag, log_message) \
177     VERIFY_NON_NULL_RET((arg), (log_tag), (log_message), CA_STATUS_INVALID_PARAM)
178
179 /**
180  * Macro to verify the validity of input argument.
181  *
182  * @param  arg  log level
183  * @param  log_tag  log tag
184  * @param  log_message  log message
185  */
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)); \
189         return; \
190     } \
191
192 #ifdef __cplusplus
193 }
194 #endif
195 #endif