Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-http.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <sol-arena.h>
22 #include <sol-buffer.h>
23 #include <sol-macros.h>
24 #include <sol-str-slice.h>
25 #include <sol-vector.h>
26 #include <inttypes.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
93 };
94 
115  SOL_HTTP_PARAM_FRAGMENT /* TODO: Should we keep this as sol_http_param_value ? */
116 };
117 
169 };
170 
178 typedef struct sol_http_params {
179 #ifndef SOL_NO_API_VERSION
180 #define SOL_HTTP_PARAM_API_VERSION (1)
181  uint16_t api_version;
182 #endif
183 
185  struct sol_arena *arena;
187 
203  double qvalue;
204  uint16_t index;
205 };
206 
215 typedef struct sol_http_param_value {
217  union {
218  struct {
221  } key_value;
222  struct {
225  } auth;
226  struct {
227  bool value;
228  } boolean;
229  struct {
230  int32_t value;
231  } integer;
232  struct {
233  struct sol_str_slice key;
234  struct sol_str_slice value;
236  } data;
237  } value;
239 
248 typedef struct sol_http_response {
249 #ifndef SOL_NO_API_VERSION
250 #define SOL_HTTP_RESPONSE_API_VERSION (1)
251  uint16_t api_version;
252 #endif
253 
254  const char *content_type;
255  const char *url;
260 
268 typedef struct sol_http_url {
276  uint32_t port;
277 } sol_http_url;
278 
279 #ifndef SOL_NO_API_VERSION
280 
287 #define SOL_HTTP_RESPONSE_CHECK_API_VERSION(response_, ...) \
288  if (SOL_UNLIKELY((response_)->api_version != \
289  SOL_HTTP_RESPONSE_API_VERSION)) { \
290  SOL_ERR("Unexpected API version (response is %" PRIu16 ", expected %" PRIu16 ")", \
291  (response_)->api_version, SOL_HTTP_RESPONSE_API_VERSION); \
292  return __VA_ARGS__; \
293  }
294 #else
295 #define SOL_HTTP_RESPONSE_CHECK_API_VERSION(response_, ...)
296 #endif
297 
305 #define SOL_HTTP_RESPONSE_CHECK_API(response_, ...) \
306  do { \
307  if (SOL_UNLIKELY(!(response_))) { \
308  SOL_WRN("Error while reaching service."); \
309  return __VA_ARGS__; \
310  } \
311  SOL_HTTP_RESPONSE_CHECK_API_VERSION((response_), __VA_ARGS__) \
312  } while (0)
313 
314 #ifndef SOL_NO_API_VERSION
315 
321 #define SOL_HTTP_RESPONSE_CHECK_API_VERSION_GOTO(response_, label) \
322  if (SOL_UNLIKELY((response_)->api_version != \
323  SOL_HTTP_RESPONSE_API_VERSION)) { \
324  SOL_ERR("Unexpected API version (response is %" PRIu16 ", expected %" PRIu16 ")", \
325  (response_)->api_version, SOL_HTTP_RESPONSE_API_VERSION); \
326  goto label; \
327  }
328 #else
329 #define SOL_HTTP_RESPONSE_CHECK_API_VERSION_GOTO(response_, label)
330 #endif
331 
339 #define SOL_HTTP_RESPONSE_CHECK_API_GOTO(response_, label) \
340  do { \
341  if (SOL_UNLIKELY(!(response_))) { \
342  SOL_WRN("Error while reaching service."); \
343  goto label; \
344  } \
345  SOL_HTTP_RESPONSE_CHECK_API_VERSION_GOTO((response_), label) \
346  } while (0)
347 
358 #define SOL_HTTP_REQUEST_PARAMS_INIT \
359  (struct sol_http_params) { \
360  SOL_SET_API_VERSION(.api_version = SOL_HTTP_PARAM_API_VERSION, ) \
361  .params = SOL_VECTOR_INIT(struct sol_http_param_value), \
362  .arena = NULL \
363  }
364 
365 #ifndef SOL_NO_API_VERSION
366 
373 #define SOL_HTTP_PARAMS_CHECK_API_VERSION(params_, ...) \
374  if (SOL_UNLIKELY((params_)->api_version != \
375  SOL_HTTP_PARAM_API_VERSION)) { \
376  SOL_ERR("Unexpected API version (response is %" PRIu16 ", expected %" PRIu16 ")", \
377  (params_)->api_version, SOL_HTTP_PARAM_API_VERSION); \
378  return __VA_ARGS__; \
379  }
380 #else
381 #define SOL_HTTP_PARAMS_CHECK_API_VERSION(params_, ...)
382 #endif
383 
384 #ifndef SOL_NO_API_VERSION
385 
391 #define SOL_HTTP_PARAMS_CHECK_API_VERSION_GOTO(params_, label_) \
392  if (SOL_UNLIKELY((params_)->api_version != \
393  SOL_HTTP_PARAM_API_VERSION)) { \
394  SOL_ERR("Unexpected API version (params is %" PRIu16 ", expected %" PRIu16 ")", \
395  (params_)->api_version, SOL_HTTP_PARAM_API_VERSION); \
396  goto label_; \
397  }
398 #else
399 #define SOL_HTTP_PARAMS_CHECK_API_VERSION_GOTO(params_, label_)
400 #endif
401 
406 #define SOL_HTTP_REQUEST_PARAM_KEY_VALUE(type_, key_, value_) \
407  (struct sol_http_param_value) { \
408  .type = type_, \
409  .value = { \
410  .key_value = { \
411  .key = sol_str_slice_from_str((key_ ? : "")), \
412  .value = sol_str_slice_from_str((value_ ? : "")) \
413  } \
414  } \
415  }
416 
421 #define SOL_HTTP_REQUEST_PARAM_BOOL(type_, setting_) \
422  (struct sol_http_param_value) { \
423  .type = type_, \
424  .value = { .boolean = { .value = (setting_) } } \
425  }
426 
431 #define SOL_HTTP_REQUEST_PARAM_COOKIE(key_, value_) \
432  SOL_HTTP_REQUEST_PARAM_KEY_VALUE(SOL_HTTP_PARAM_COOKIE, key_, value_)
433 
438 #define SOL_HTTP_REQUEST_PARAM_HEADER(header_, content_) \
439  SOL_HTTP_REQUEST_PARAM_KEY_VALUE(SOL_HTTP_PARAM_HEADER, header_, content_)
440 
445 #define SOL_HTTP_REQUEST_PARAM_AUTH_BASIC(username_, password_) \
446  (struct sol_http_param_value) { \
447  .type = SOL_HTTP_PARAM_AUTH_BASIC, \
448  .value = { \
449  .auth = { \
450  .user = sol_str_slice_from_str((username_ ? : "")), \
451  .password = sol_str_slice_from_str((password_ ? : "")) \
452  } \
453  } \
454  }
455 
460 #define SOL_HTTP_REQUEST_PARAM_QUERY(key_, value_) \
461  SOL_HTTP_REQUEST_PARAM_KEY_VALUE(SOL_HTTP_PARAM_QUERY_PARAM, key_, value_)
462 
467 #define SOL_HTTP_REQUEST_PARAM_POST_FIELD(key_, value_) \
468  SOL_HTTP_REQUEST_PARAM_KEY_VALUE(SOL_HTTP_PARAM_POST_FIELD, key_, value_)
469 
474 #define SOL_HTTP_REQUEST_PARAM_ALLOW_REDIR(setting_) \
475  SOL_HTTP_REQUEST_PARAM_BOOL(SOL_HTTP_PARAM_ALLOW_REDIR, setting_)
476 
481 #define SOL_HTTP_REQUEST_PARAM_VERBOSE(setting_) \
482  SOL_HTTP_REQUEST_PARAM_BOOL(SOL_HTTP_PARAM_VERBOSE, setting_)
483 
488 #define SOL_HTTP_REQUEST_PARAM_TIMEOUT(setting_) \
489  (struct sol_http_param_value) { \
490  .type = SOL_HTTP_PARAM_TIMEOUT, \
491  .value = { \
492  .integer = { .value = (setting_) } \
493  } \
494  }
495 
496 
501 #define SOL_HTTP_REQUEST_PARAM_POST_DATA_FILE(key_, filename_) \
502  (struct sol_http_param_value) { \
503  .type = SOL_HTTP_PARAM_POST_DATA, \
504  .value = { \
505  .data = { \
506  .key = sol_str_slice_from_str((key_ ? : "")), \
507  .value = SOL_STR_SLICE_EMPTY, \
508  .filename = sol_str_slice_from_str((filename_ ? : "")) \
509  } \
510  } \
511  }
512 
517 #define SOL_HTTP_REQUEST_PARAM_POST_DATA_CONTENTS(key_, value_) \
518  (struct sol_http_param_value) { \
519  .type = SOL_HTTP_PARAM_POST_DATA, \
520  .value = { \
521  .data = { \
522  .key = sol_str_slice_from_str((key_ ? : "")), \
523  .value = (value_), \
524  .filename = SOL_STR_SLICE_EMPTY, \
525  } \
526  } \
527  }
528 
543 #define SOL_HTTP_PARAMS_FOREACH_IDX(param, itrvar, idx) \
544  for (idx = 0; \
545  param && idx < (param)->params.len && (itrvar = (struct sol_http_param_value *)sol_vector_get(&(param)->params, idx), true); \
546  idx++)
547 
555 static inline void
557 {
558  sol_vector_init(&params->params, sizeof(struct sol_http_param_value));
559  params->arena = NULL;
561 }
562 
574 int sol_http_params_add(struct sol_http_params *params,
576 #ifndef DOXYGEN_RUN
578 #endif
579  ;
580 
592 int sol_http_params_add_copy(struct sol_http_params *params, struct sol_http_param_value value);
593 
604 void sol_http_params_clear(struct sol_http_params *params);
605 
621 int sol_http_encode_slice(struct sol_buffer *buf, const struct sol_str_slice value);
622 
638 int sol_http_decode_slice(struct sol_buffer *buf, const struct sol_str_slice value);
639 
649 int sol_http_create_full_uri(struct sol_buffer *buf, const struct sol_http_url url, const struct sol_http_params *params);
650 
661 int sol_http_create_uri(struct sol_buffer *buf, const struct sol_str_slice base_uri, const struct sol_http_params *params);
662 
674 int sol_http_encode_params(struct sol_buffer *buf, enum sol_http_param_type type, const struct sol_http_params *params);
675 
687 int sol_http_decode_params(const struct sol_str_slice params_slice, enum sol_http_param_type type, struct sol_http_params *params);
688 
702 int sol_http_split_uri(const struct sol_str_slice full_uri, struct sol_http_url *url);
703 
713 static inline int
714 sol_http_create_uri_from_str(struct sol_buffer *buf, const char *base_url, const struct sol_http_params *params)
715 {
716  return sol_http_create_uri(buf, sol_str_slice_from_str(base_url ? base_url : ""), params);
717 }
718 
730 int sol_http_split_query(const char *query, struct sol_http_params *params);
731 
743 int sol_http_split_post_field(const char *query, struct sol_http_params *params);
744 
755 int sol_http_parse_content_type_priorities(const struct sol_str_slice content_type, struct sol_vector *priorities);
756 
764 
769 #ifdef __cplusplus
770 }
771 #endif
int sol_http_params_add(struct sol_http_params *params, struct sol_http_param_value value)
Add a new parameter to HTTP parameters vector.
Handle for an HTTP response.
Definition: sol-http.h:248
Definition: sol-http.h:111
#define SOL_HTTP_PARAM_API_VERSION
Definition: sol-http.h:180
The request has been accepted for processing.
Definition: sol-http.h:127
struct sol_http_param_value::@3::@6 boolean
int response_code
Definition: sol-http.h:258
client makes an HTTP request by using an HTTP method that does not comply with the HTTP specification...
Definition: sol-http.h:92
double qvalue
The qvalue for the content type.
Definition: sol-http.h:203
struct sol_str_slice content_type
The content type itself.
Definition: sol-http.h:195
int sol_http_split_uri(const struct sol_str_slice full_uri, struct sol_http_url *url)
Split an URI.
uint16_t index
The original index as found in the content type/accept HTTP header.
Definition: sol-http.h:204
uint16_t api_version
Definition: sol-http.h:181
Definition: sol-http.h:110
struct sol_arena * arena
arena with copied parameter slices
Definition: sol-http.h:185
struct sol_http_param_value::@3::@7 integer
struct sol_http_response sol_http_response
Handle for an HTTP response.
struct sol_str_slice key
Definition: sol-http.h:219
Submits data to be processed to a specified resource.
Definition: sol-http.h:55
struct sol_str_slice query
Definition: sol-http.h:274
struct sol_str_slice filename
Definition: sol-http.h:235
Used to rank content type priorities.
Definition: sol-http.h:194
Same as GET, but transfers the status line and header section only.
Definition: sol-http.h:60
The requested resource could not be found.
Definition: sol-http.h:155
Performs a message loop-back test along the path to the target resource.
Definition: sol-http.h:83
struct sol_http_param_value::@3::@5 auth
These are routines that Soletta provides for its arena implementation.
int sol_http_decode_slice(struct sol_buffer *buf, const struct sol_str_slice value)
Decodes an URL string.
struct sol_str_slice password
Definition: sol-http.h:224
struct sol_str_slice password
Definition: sol-http.h:271
Handle for an HTTP URL.
Definition: sol-http.h:268
struct sol_str_slice user
Definition: sol-http.h:223
Definition: sol-http.h:108
struct sol_vector params
vector of parameters, struct sol_http_param_value
Definition: sol-http.h:184
Definition: sol-http.h:113
These are common Soletta macros.
int sol_http_encode_slice(struct sol_buffer *buf, const struct sol_str_slice value)
Encodes an URL string.
struct sol_vector tokens
An array of sol_str_slice.
Definition: sol-http.h:198
Definition: sol-http.h:107
static struct sol_buffer value
Definition: server.c:42
int sol_http_encode_params(struct sol_buffer *buf, enum sol_http_param_type type, const struct sol_http_params *params)
Encodes HTTP parameters of a given type.
struct sol_buffer content
Definition: sol-http.h:256
Definition: sol-http.h:106
Definition: sol-http.h:114
int sol_http_parse_content_type_priorities(const struct sol_str_slice content_type, struct sol_vector *priorities)
Sort the content type slice based on its priorities.
String slice type.
Definition: sol-str-slice.h:84
struct sol_str_slice scheme
Definition: sol-http.h:269
These are routines that Soletta provides for its buffer implementation.
Definition: sol-http.h:109
struct sol_str_slice user
Definition: sol-http.h:270
#define SOL_ATTR_WARN_UNUSED_RESULT
Causes a warning to be emitted if a caller of the function with this attribute does not use its retur...
Definition: sol-macros.h:187
int sol_http_split_post_field(const char *query, struct sol_http_params *params)
Split post/field string into parameters.
enum sol_http_param_type type
Definition: sol-http.h:216
int32_t value
Definition: sol-http.h:230
struct sol_http_params param
Definition: sol-http.h:257
sol_http_status_code
Status codes as defined by HTTP protocol.
Definition: sol-http.h:123
void sol_http_params_clear(struct sol_http_params *params)
Clear vector of HTTP parameters.
static struct sol_str_slice sol_str_slice_from_str(const char *s)
Populates a slice from a string.
Definition: sol-str-slice.h:238
struct sol_arena sol_arena
Sol Arena type.
Definition: sol-arena.h:54
struct sol_http_url sol_http_url
Handle for an HTTP URL.
Used to define an HTTP parameter.
Definition: sol-http.h:215
struct sol_str_slice sub_type
The sub type.
Definition: sol-http.h:197
int sol_http_decode_params(const struct sol_str_slice params_slice, enum sol_http_param_type type, struct sol_http_params *params)
Decodes HTTP parameters of a given type.
sol_http_method
Type of HTTP method.
Definition: sol-http.h:47
Keep vector of HTTP parameters to be sent in a request.
Definition: sol-http.h:178
int sol_http_create_uri(struct sol_buffer *buf, const struct sol_str_slice base_uri, const struct sol_http_params *params)
A simpler version of sol_http_create_full_uri().
Definition: sol-http.h:112
The request was a valid request, but the server is refusing to respond to it.
Definition: sol-http.h:151
Establishes a tunnel to the server identified by a given URI.
Definition: sol-http.h:74
sol_http_param_type
Type of HTTP parameter.
Definition: sol-http.h:105
struct sol_str_slice host
Definition: sol-http.h:272
struct sol_str_slice fragment
Definition: sol-http.h:275
Definition: sol-http.h:115
Replaces all current representations of the target resource with the uploaded content.
Definition: sol-http.h:70
void sol_vector_init(struct sol_vector *v, uint16_t elem_size)
Initializes a sol_vector structure.
int sol_http_create_full_uri(struct sol_buffer *buf, const struct sol_http_url url, const struct sol_http_params *params)
Creates an URI based on struct sol_http_url and its parameters.
struct sol_http_param_value::@3::@8 data
struct sol_http_params sol_http_params
Keep vector of HTTP parameters to be sent in a request.
struct sol_str_slice path
Definition: sol-http.h:273
const char * content_type
Definition: sol-http.h:254
int sol_http_split_query(const char *query, struct sol_http_params *params)
Split query into parameters.
#define SOL_SET_API_VERSION(expression)
This macro will cope with SOL_NO_API_VERSION and allows easy declaration of api_version fields...
Definition: sol-mainloop.h:660
A generic error message.
Definition: sol-http.h:163
Describes the communication options for the target resource.
Definition: sol-http.h:78
These are routines that Soletta provides for its string slice implementation.
uint32_t port
If set to 0 it'll be ignored.
Definition: sol-http.h:276
const char * url
Definition: sol-http.h:255
int sol_http_params_add_copy(struct sol_http_params *params, struct sol_http_param_value value)
Add a new parameter to HTTP parameters vector copying strings.
static void sol_http_params_init(struct sol_http_params *params)
Initialize HTTP parameters struct with an empty vector.
Definition: sol-http.h:556
struct sol_str_slice value
Definition: sol-http.h:220
uint16_t api_version
Definition: sol-http.h:251
Requests data from a specified resource.
Definition: sol-http.h:51
Soletta vector is an array that grows dynamically.
Definition: sol-vector.h:58
static int sol_http_create_uri_from_str(struct sol_buffer *buf, const char *base_url, const struct sol_http_params *params)
A wrapper on top of sol_http_create_uri()
Definition: sol-http.h:714
Provides an URL in the location header field.
Definition: sol-http.h:131
Indicates that the resource has not been modified since the version specified by the request headers...
Definition: sol-http.h:141
struct sol_str_slice type
The type.
Definition: sol-http.h:196
These are routines that Soletta provides for its vector implementation.
bool value
Definition: sol-http.h:227
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
The response to the request can be found under another URI using a GET method.
Definition: sol-http.h:136
The server either does not recognize the request method, or it lacks the ability to fulfill the reque...
Definition: sol-http.h:168
The server cannot or will not process the request due to an apparent client error.
Definition: sol-http.h:146
Used to update partial resources.
Definition: sol-http.h:87
void sol_http_content_type_priorities_array_clear(struct sol_vector *priorities)
Clears the priorities array.
A request method is not supported for the requested resource.
Definition: sol-http.h:159
Removes all current representations of the target resource given by a URI.
Definition: sol-http.h:65
struct sol_http_param_value::@3::@4 key_value
struct sol_http_param_value sol_http_param_value
Used to define an HTTP parameter.