Microsoft Azure IoTHub client HTTP transport
Dependents: iothub_client_sample_http simplesample_http temp_sensor_anomaly
This library implements the HTTP transport for Microsoft Azure IoTHub client. The code is replicated from https://github.com/Azure/azure-iot-sdks
iothubtransporthttp.c@10:b012891d0a54, 2016-01-28 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Jan 28 09:57:44 2016 -0800
- Revision:
- 10:b012891d0a54
- Parent:
- 9:0b2cab6cfc60
- Child:
- 11:495deb847d9c
v1.0.0-preview.9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 6:73793bae15ba | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 6:73793bae15ba | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 6:73793bae15ba | 3 | |
AzureIoTClient | 6:73793bae15ba | 4 | #include <stdlib.h> |
AzureIoTClient | 6:73793bae15ba | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 6:73793bae15ba | 6 | #include <crtdbg.h> |
AzureIoTClient | 6:73793bae15ba | 7 | #endif |
AzureIoTClient | 6:73793bae15ba | 8 | #include "gballoc.h" |
AzureIoTClient | 6:73793bae15ba | 9 | |
AzureIoTClient | 6:73793bae15ba | 10 | #include <time.h> |
AzureIoTClient | 9:0b2cab6cfc60 | 11 | #include "version.h" |
AzureIoTClient | 6:73793bae15ba | 12 | #include "iothub_client_private.h" |
AzureIoTClient | 6:73793bae15ba | 13 | #include "iothubtransporthttp.h" |
AzureIoTClient | 6:73793bae15ba | 14 | |
AzureIoTClient | 6:73793bae15ba | 15 | #include "httpapiexsas.h" |
AzureIoTClient | 6:73793bae15ba | 16 | #include "urlencode.h" |
AzureIoTClient | 6:73793bae15ba | 17 | #include "iot_logging.h" |
AzureIoTClient | 6:73793bae15ba | 18 | #include "httpapiex.h" |
AzureIoTClient | 6:73793bae15ba | 19 | #include "httpapiexsas.h" |
AzureIoTClient | 6:73793bae15ba | 20 | #include "strings.h" |
AzureIoTClient | 6:73793bae15ba | 21 | #include "base64.h" |
AzureIoTClient | 6:73793bae15ba | 22 | #include "doublylinkedlist.h" |
AzureIoTClient | 6:73793bae15ba | 23 | #include "httpheaders.h" |
AzureIoTClient | 6:73793bae15ba | 24 | #include "agenttime.h" |
AzureIoTClient | 6:73793bae15ba | 25 | |
AzureIoTClient | 6:73793bae15ba | 26 | #define IOTHUB_APP_PREFIX "iothub-app-" |
AzureIoTClient | 7:48f0f78cd3ef | 27 | const char* IOTHUB_MESSAGE_ID = "iothub-messageid"; |
AzureIoTClient | 7:48f0f78cd3ef | 28 | const char* IOTHUB_CORRELATION_ID = "iothub-correlationid"; |
AzureIoTClient | 6:73793bae15ba | 29 | |
AzureIoTClient | 6:73793bae15ba | 30 | #define CONTENT_TYPE "Content-Type" |
AzureIoTClient | 6:73793bae15ba | 31 | #define APPLICATION_OCTET_STREAM "application/octet-stream" |
AzureIoTClient | 6:73793bae15ba | 32 | #define APPLICATION_VND_MICROSOFT_IOTHUB_JSON "application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 33 | |
AzureIoTClient | 6:73793bae15ba | 34 | /*DEFAULT_GETMINIMUMPOLLINGTIME is the minimum time in seconds allowed between 2 consecutive GET issues to the service (GET=fetch messages)*/ |
AzureIoTClient | 6:73793bae15ba | 35 | /*the default is 25 minutes*/ |
AzureIoTClient | 6:73793bae15ba | 36 | #define DEFAULT_GETMINIMUMPOLLINGTIME ((unsigned int)25*60) |
AzureIoTClient | 6:73793bae15ba | 37 | |
AzureIoTClient | 6:73793bae15ba | 38 | #define MAXIMUM_MESSAGE_SIZE (255*1024-1) |
AzureIoTClient | 6:73793bae15ba | 39 | #define MAXIMUM_PAYLOAD_OVERHEAD 384 |
AzureIoTClient | 6:73793bae15ba | 40 | #define MAXIMUM_PROPERTY_OVERHEAD 16 |
AzureIoTClient | 6:73793bae15ba | 41 | |
AzureIoTClient | 6:73793bae15ba | 42 | /*forward declaration*/ |
AzureIoTClient | 6:73793bae15ba | 43 | static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count); |
AzureIoTClient | 6:73793bae15ba | 44 | |
AzureIoTClient | 6:73793bae15ba | 45 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_069: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:] */ |
AzureIoTClient | 6:73793bae15ba | 46 | static TRANSPORT_PROVIDER thisTransportProvider = |
AzureIoTClient | 6:73793bae15ba | 47 | { |
AzureIoTClient | 6:73793bae15ba | 48 | IoTHubTransportHttp_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption; */ |
AzureIoTClient | 6:73793bae15ba | 49 | IoTHubTransportHttp_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create; */ |
AzureIoTClient | 6:73793bae15ba | 50 | IoTHubTransportHttp_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy; */ |
AzureIoTClient | 6:73793bae15ba | 51 | IoTHubTransportHttp_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe; */ |
AzureIoTClient | 6:73793bae15ba | 52 | IoTHubTransportHttp_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe; */ |
AzureIoTClient | 6:73793bae15ba | 53 | IoTHubTransportHttp_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork; */ |
AzureIoTClient | 6:73793bae15ba | 54 | IoTHubTransportHttp_GetSendStatus /* pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus */ |
AzureIoTClient | 6:73793bae15ba | 55 | }; |
AzureIoTClient | 6:73793bae15ba | 56 | |
AzureIoTClient | 6:73793bae15ba | 57 | const void* HTTP_Protocol(void) |
AzureIoTClient | 6:73793bae15ba | 58 | { |
AzureIoTClient | 6:73793bae15ba | 59 | return &thisTransportProvider; |
AzureIoTClient | 6:73793bae15ba | 60 | } |
AzureIoTClient | 6:73793bae15ba | 61 | |
AzureIoTClient | 6:73793bae15ba | 62 | typedef struct HTTPTRANSPORT_HANDLE_DATA_TAG |
AzureIoTClient | 6:73793bae15ba | 63 | { |
AzureIoTClient | 6:73793bae15ba | 64 | STRING_HANDLE eventHTTPrelativePath; |
AzureIoTClient | 6:73793bae15ba | 65 | STRING_HANDLE messageHTTPrelativePath; |
AzureIoTClient | 6:73793bae15ba | 66 | HTTP_HEADERS_HANDLE eventHTTPrequestHeaders; |
AzureIoTClient | 6:73793bae15ba | 67 | STRING_HANDLE hostName; |
AzureIoTClient | 6:73793bae15ba | 68 | HTTPAPIEX_HANDLE httpApiExHandle; |
AzureIoTClient | 6:73793bae15ba | 69 | HTTP_HEADERS_HANDLE messageHTTPrequestHeaders; |
AzureIoTClient | 6:73793bae15ba | 70 | STRING_HANDLE abandonHTTPrelativePathBegin; |
AzureIoTClient | 6:73793bae15ba | 71 | HTTPAPIEX_SAS_HANDLE sasObject; |
AzureIoTClient | 6:73793bae15ba | 72 | bool DoWork_PullMessage; |
AzureIoTClient | 6:73793bae15ba | 73 | bool doBatchedTransfers; |
AzureIoTClient | 6:73793bae15ba | 74 | bool isFirstPoll; |
AzureIoTClient | 6:73793bae15ba | 75 | unsigned int getMinimumPollingTime; |
AzureIoTClient | 6:73793bae15ba | 76 | time_t lastPollTime; |
AzureIoTClient | 6:73793bae15ba | 77 | PDLIST_ENTRY waitingToSend; |
AzureIoTClient | 6:73793bae15ba | 78 | DLIST_ENTRY eventConfirmations; /*holds items for event confirmations*/ |
AzureIoTClient | 6:73793bae15ba | 79 | }HTTPTRANSPORT_HANDLE_DATA; |
AzureIoTClient | 6:73793bae15ba | 80 | |
AzureIoTClient | 6:73793bae15ba | 81 | |
AzureIoTClient | 6:73793bae15ba | 82 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 83 | static void destroy_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 84 | { |
AzureIoTClient | 6:73793bae15ba | 85 | STRING_delete(handleData->eventHTTPrelativePath); |
AzureIoTClient | 6:73793bae15ba | 86 | handleData->eventHTTPrelativePath = NULL; |
AzureIoTClient | 6:73793bae15ba | 87 | } |
AzureIoTClient | 6:73793bae15ba | 88 | |
AzureIoTClient | 6:73793bae15ba | 89 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 90 | static bool create_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 91 | { |
AzureIoTClient | 6:73793bae15ba | 92 | bool result; |
AzureIoTClient | 6:73793bae15ba | 93 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 94 | handleData->eventHTTPrelativePath = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 95 | if (handleData->eventHTTPrelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 96 | { |
AzureIoTClient | 6:73793bae15ba | 97 | result = false; |
AzureIoTClient | 6:73793bae15ba | 98 | } |
AzureIoTClient | 6:73793bae15ba | 99 | else if (!( |
AzureIoTClient | 6:73793bae15ba | 100 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 101 | (STRING_concat_with_STRING(handleData->eventHTTPrelativePath, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 102 | (STRING_concat(handleData->eventHTTPrelativePath, EVENT_ENDPOINT API_VERSION) == 0) |
AzureIoTClient | 6:73793bae15ba | 103 | )) |
AzureIoTClient | 6:73793bae15ba | 104 | { |
AzureIoTClient | 6:73793bae15ba | 105 | destroy_eventHTTPrelativePath(handleData); |
AzureIoTClient | 6:73793bae15ba | 106 | result = false; |
AzureIoTClient | 6:73793bae15ba | 107 | } |
AzureIoTClient | 6:73793bae15ba | 108 | else |
AzureIoTClient | 6:73793bae15ba | 109 | { |
AzureIoTClient | 6:73793bae15ba | 110 | result = true; |
AzureIoTClient | 6:73793bae15ba | 111 | } |
AzureIoTClient | 6:73793bae15ba | 112 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 113 | return result; |
AzureIoTClient | 6:73793bae15ba | 114 | } |
AzureIoTClient | 6:73793bae15ba | 115 | |
AzureIoTClient | 6:73793bae15ba | 116 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "message HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 117 | static void destroy_messageHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 118 | { |
AzureIoTClient | 6:73793bae15ba | 119 | STRING_delete(handleData->messageHTTPrelativePath); |
AzureIoTClient | 6:73793bae15ba | 120 | handleData->messageHTTPrelativePath = NULL; |
AzureIoTClient | 6:73793bae15ba | 121 | } |
AzureIoTClient | 6:73793bae15ba | 122 | |
AzureIoTClient | 6:73793bae15ba | 123 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "message HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 124 | static bool create_messageHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 125 | { |
AzureIoTClient | 6:73793bae15ba | 126 | bool result; |
AzureIoTClient | 6:73793bae15ba | 127 | handleData->messageHTTPrelativePath = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 128 | if (handleData->messageHTTPrelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 129 | { |
AzureIoTClient | 6:73793bae15ba | 130 | result = false; |
AzureIoTClient | 6:73793bae15ba | 131 | } |
AzureIoTClient | 6:73793bae15ba | 132 | else |
AzureIoTClient | 6:73793bae15ba | 133 | { |
AzureIoTClient | 6:73793bae15ba | 134 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 135 | if (!( |
AzureIoTClient | 6:73793bae15ba | 136 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 137 | (STRING_concat_with_STRING(handleData->messageHTTPrelativePath, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 138 | (STRING_concat(handleData->messageHTTPrelativePath, MESSAGE_ENDPOINT_HTTP API_VERSION) == 0) |
AzureIoTClient | 6:73793bae15ba | 139 | )) |
AzureIoTClient | 6:73793bae15ba | 140 | { |
AzureIoTClient | 6:73793bae15ba | 141 | result = false; |
AzureIoTClient | 6:73793bae15ba | 142 | destroy_messageHTTPrelativePath(handleData); |
AzureIoTClient | 6:73793bae15ba | 143 | } |
AzureIoTClient | 6:73793bae15ba | 144 | else |
AzureIoTClient | 6:73793bae15ba | 145 | { |
AzureIoTClient | 6:73793bae15ba | 146 | result = true; |
AzureIoTClient | 6:73793bae15ba | 147 | } |
AzureIoTClient | 6:73793bae15ba | 148 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 149 | } |
AzureIoTClient | 6:73793bae15ba | 150 | |
AzureIoTClient | 6:73793bae15ba | 151 | return result; |
AzureIoTClient | 6:73793bae15ba | 152 | } |
AzureIoTClient | 6:73793bae15ba | 153 | |
AzureIoTClient | 6:73793bae15ba | 154 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 155 | "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events"; |
AzureIoTClient | 6:73793bae15ba | 156 | "Authorization":" " |
AzureIoTClient | 6:73793bae15ba | 157 | "Content-Type":"application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 158 | "Accept":"application/json" |
AzureIoTClient | 6:73793bae15ba | 159 | "Connection":"Keep-Alive"]*/ |
AzureIoTClient | 6:73793bae15ba | 160 | static void destroy_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 161 | { |
AzureIoTClient | 6:73793bae15ba | 162 | HTTPHeaders_Free(handleData->eventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 163 | handleData->eventHTTPrequestHeaders = NULL; |
AzureIoTClient | 6:73793bae15ba | 164 | } |
AzureIoTClient | 6:73793bae15ba | 165 | |
AzureIoTClient | 6:73793bae15ba | 166 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 167 | "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events"; |
AzureIoTClient | 6:73793bae15ba | 168 | "Authorization":" " |
AzureIoTClient | 6:73793bae15ba | 169 | "Content-Type":"application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 170 | "Accept":"application/json" |
AzureIoTClient | 9:0b2cab6cfc60 | 171 | "Connection":"Keep-Alive" |
AzureIoTClient | 9:0b2cab6cfc60 | 172 | "User-Agent":"iothubclient1.0.0-preview.7]*/ |
AzureIoTClient | 6:73793bae15ba | 173 | static bool create_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 174 | { |
AzureIoTClient | 6:73793bae15ba | 175 | bool result; |
AzureIoTClient | 6:73793bae15ba | 176 | handleData->eventHTTPrequestHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 177 | if (handleData->eventHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 178 | { |
AzureIoTClient | 6:73793bae15ba | 179 | result = false; |
AzureIoTClient | 6:73793bae15ba | 180 | } |
AzureIoTClient | 6:73793bae15ba | 181 | else |
AzureIoTClient | 6:73793bae15ba | 182 | { |
AzureIoTClient | 6:73793bae15ba | 183 | STRING_HANDLE temp = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 184 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 185 | { |
AzureIoTClient | 6:73793bae15ba | 186 | result = false; |
AzureIoTClient | 6:73793bae15ba | 187 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 188 | } |
AzureIoTClient | 6:73793bae15ba | 189 | else |
AzureIoTClient | 6:73793bae15ba | 190 | { |
AzureIoTClient | 6:73793bae15ba | 191 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 192 | if (!( |
AzureIoTClient | 6:73793bae15ba | 193 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 194 | (STRING_concat_with_STRING(temp, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 195 | (STRING_concat(temp, EVENT_ENDPOINT) == 0) |
AzureIoTClient | 6:73793bae15ba | 196 | )) |
AzureIoTClient | 6:73793bae15ba | 197 | { |
AzureIoTClient | 6:73793bae15ba | 198 | result = false; |
AzureIoTClient | 6:73793bae15ba | 199 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 200 | } |
AzureIoTClient | 6:73793bae15ba | 201 | else |
AzureIoTClient | 6:73793bae15ba | 202 | { |
AzureIoTClient | 6:73793bae15ba | 203 | if (!( |
AzureIoTClient | 6:73793bae15ba | 204 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "iothub-to", STRING_c_str(temp)) == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 205 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Authorization", " ") == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 206 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) && |
AzureIoTClient | 9:0b2cab6cfc60 | 207 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Connection", "Keep-Alive") == HTTP_HEADERS_OK) && |
AzureIoTClient | 10:b012891d0a54 | 208 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "User-Agent", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 209 | )) |
AzureIoTClient | 6:73793bae15ba | 210 | { |
AzureIoTClient | 6:73793bae15ba | 211 | result = false; |
AzureIoTClient | 6:73793bae15ba | 212 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 213 | } |
AzureIoTClient | 6:73793bae15ba | 214 | else |
AzureIoTClient | 6:73793bae15ba | 215 | { |
AzureIoTClient | 6:73793bae15ba | 216 | result = true; |
AzureIoTClient | 6:73793bae15ba | 217 | } |
AzureIoTClient | 6:73793bae15ba | 218 | } |
AzureIoTClient | 6:73793bae15ba | 219 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 220 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 221 | } |
AzureIoTClient | 6:73793bae15ba | 222 | } |
AzureIoTClient | 6:73793bae15ba | 223 | return result; |
AzureIoTClient | 6:73793bae15ba | 224 | } |
AzureIoTClient | 6:73793bae15ba | 225 | |
AzureIoTClient | 6:73793bae15ba | 226 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 227 | static void destroy_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 228 | { |
AzureIoTClient | 6:73793bae15ba | 229 | STRING_delete(handleData->hostName); |
AzureIoTClient | 6:73793bae15ba | 230 | handleData->hostName = NULL; |
AzureIoTClient | 6:73793bae15ba | 231 | } |
AzureIoTClient | 6:73793bae15ba | 232 | |
AzureIoTClient | 6:73793bae15ba | 233 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 234 | static bool create_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 235 | { |
AzureIoTClient | 6:73793bae15ba | 236 | bool result; |
AzureIoTClient | 6:73793bae15ba | 237 | handleData->hostName = STRING_construct(config->upperConfig->iotHubName); |
AzureIoTClient | 6:73793bae15ba | 238 | if (handleData->hostName == NULL) |
AzureIoTClient | 6:73793bae15ba | 239 | { |
AzureIoTClient | 6:73793bae15ba | 240 | result = false; |
AzureIoTClient | 6:73793bae15ba | 241 | } |
AzureIoTClient | 6:73793bae15ba | 242 | else |
AzureIoTClient | 6:73793bae15ba | 243 | { |
AzureIoTClient | 6:73793bae15ba | 244 | if ((STRING_concat(handleData->hostName, ".") != 0) || |
AzureIoTClient | 6:73793bae15ba | 245 | (STRING_concat(handleData->hostName, config->upperConfig->iotHubSuffix) != 0)) |
AzureIoTClient | 6:73793bae15ba | 246 | { |
AzureIoTClient | 6:73793bae15ba | 247 | destroy_hostName(handleData); |
AzureIoTClient | 6:73793bae15ba | 248 | result = false; |
AzureIoTClient | 6:73793bae15ba | 249 | } |
AzureIoTClient | 6:73793bae15ba | 250 | else |
AzureIoTClient | 6:73793bae15ba | 251 | { |
AzureIoTClient | 6:73793bae15ba | 252 | result = true; |
AzureIoTClient | 6:73793bae15ba | 253 | } |
AzureIoTClient | 6:73793bae15ba | 254 | } |
AzureIoTClient | 6:73793bae15ba | 255 | return result; |
AzureIoTClient | 6:73793bae15ba | 256 | } |
AzureIoTClient | 6:73793bae15ba | 257 | |
AzureIoTClient | 6:73793bae15ba | 258 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_009: [Otherwise, IoTHubTransportHttp_Create shall create a HTTPAPIEX_HANDLE by a call to HTTPAPIEX_Create passing for hostName the hostname so far constructed by IoTHubTransportHttp_Create.]*/ |
AzureIoTClient | 6:73793bae15ba | 259 | static void destroy_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 260 | { |
AzureIoTClient | 6:73793bae15ba | 261 | HTTPAPIEX_Destroy(handleData->httpApiExHandle); |
AzureIoTClient | 6:73793bae15ba | 262 | handleData->httpApiExHandle = NULL; |
AzureIoTClient | 6:73793bae15ba | 263 | } |
AzureIoTClient | 6:73793bae15ba | 264 | |
AzureIoTClient | 6:73793bae15ba | 265 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 266 | static bool create_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 267 | { |
AzureIoTClient | 6:73793bae15ba | 268 | bool result; |
AzureIoTClient | 6:73793bae15ba | 269 | (void)config; |
AzureIoTClient | 6:73793bae15ba | 270 | handleData->httpApiExHandle = HTTPAPIEX_Create(STRING_c_str(handleData->hostName)); |
AzureIoTClient | 6:73793bae15ba | 271 | if (handleData->httpApiExHandle == NULL) |
AzureIoTClient | 6:73793bae15ba | 272 | { |
AzureIoTClient | 6:73793bae15ba | 273 | result = false; |
AzureIoTClient | 6:73793bae15ba | 274 | } |
AzureIoTClient | 6:73793bae15ba | 275 | else |
AzureIoTClient | 6:73793bae15ba | 276 | { |
AzureIoTClient | 6:73793bae15ba | 277 | result = true; |
AzureIoTClient | 6:73793bae15ba | 278 | } |
AzureIoTClient | 6:73793bae15ba | 279 | return result; |
AzureIoTClient | 6:73793bae15ba | 280 | } |
AzureIoTClient | 6:73793bae15ba | 281 | |
AzureIoTClient | 6:73793bae15ba | 282 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "message HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 283 | "Authorization": " "]*/ |
AzureIoTClient | 6:73793bae15ba | 284 | static void destroy_messageHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 285 | { |
AzureIoTClient | 6:73793bae15ba | 286 | HTTPHeaders_Free(handleData->messageHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 287 | handleData->messageHTTPrequestHeaders = NULL; |
AzureIoTClient | 6:73793bae15ba | 288 | } |
AzureIoTClient | 6:73793bae15ba | 289 | |
AzureIoTClient | 6:73793bae15ba | 290 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "message HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 291 | "Authorization": " "]*/ |
AzureIoTClient | 6:73793bae15ba | 292 | static bool create_messageHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 293 | { |
AzureIoTClient | 6:73793bae15ba | 294 | bool result; |
AzureIoTClient | 6:73793bae15ba | 295 | (void)config; |
AzureIoTClient | 6:73793bae15ba | 296 | handleData->messageHTTPrequestHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 297 | if (handleData->messageHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 298 | { |
AzureIoTClient | 6:73793bae15ba | 299 | result = false; |
AzureIoTClient | 6:73793bae15ba | 300 | } |
AzureIoTClient | 6:73793bae15ba | 301 | else |
AzureIoTClient | 6:73793bae15ba | 302 | { |
AzureIoTClient | 6:73793bae15ba | 303 | if (HTTPHeaders_AddHeaderNameValuePair(handleData->messageHTTPrequestHeaders, "Authorization", " ") != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 304 | { |
AzureIoTClient | 6:73793bae15ba | 305 | destroy_messageHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 306 | result = false; |
AzureIoTClient | 6:73793bae15ba | 307 | } |
AzureIoTClient | 6:73793bae15ba | 308 | else |
AzureIoTClient | 6:73793bae15ba | 309 | { |
AzureIoTClient | 6:73793bae15ba | 310 | result = true; |
AzureIoTClient | 6:73793bae15ba | 311 | } |
AzureIoTClient | 6:73793bae15ba | 312 | } |
AzureIoTClient | 6:73793bae15ba | 313 | return result; |
AzureIoTClient | 6:73793bae15ba | 314 | } |
AzureIoTClient | 6:73793bae15ba | 315 | |
AzureIoTClient | 6:73793bae15ba | 316 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */ |
AzureIoTClient | 6:73793bae15ba | 317 | static void destroy_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 318 | { |
AzureIoTClient | 6:73793bae15ba | 319 | STRING_delete(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 320 | handleData->abandonHTTPrelativePathBegin = NULL; |
AzureIoTClient | 6:73793bae15ba | 321 | } |
AzureIoTClient | 6:73793bae15ba | 322 | |
AzureIoTClient | 6:73793bae15ba | 323 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */ |
AzureIoTClient | 6:73793bae15ba | 324 | static bool create_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 325 | { |
AzureIoTClient | 6:73793bae15ba | 326 | bool result; |
AzureIoTClient | 6:73793bae15ba | 327 | handleData->abandonHTTPrelativePathBegin = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 328 | if (handleData->abandonHTTPrelativePathBegin == NULL) |
AzureIoTClient | 6:73793bae15ba | 329 | { |
AzureIoTClient | 6:73793bae15ba | 330 | result = false; |
AzureIoTClient | 6:73793bae15ba | 331 | } |
AzureIoTClient | 6:73793bae15ba | 332 | else |
AzureIoTClient | 6:73793bae15ba | 333 | { |
AzureIoTClient | 6:73793bae15ba | 334 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 335 | if (!( |
AzureIoTClient | 6:73793bae15ba | 336 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 337 | (STRING_concat_with_STRING(handleData->abandonHTTPrelativePathBegin, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 338 | (STRING_concat(handleData->abandonHTTPrelativePathBegin, MESSAGE_ENDPOINT_HTTP_ETAG) == 0) |
AzureIoTClient | 6:73793bae15ba | 339 | )) |
AzureIoTClient | 6:73793bae15ba | 340 | { |
AzureIoTClient | 6:73793bae15ba | 341 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 342 | STRING_delete(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 343 | result = false; |
AzureIoTClient | 6:73793bae15ba | 344 | } |
AzureIoTClient | 6:73793bae15ba | 345 | else |
AzureIoTClient | 6:73793bae15ba | 346 | { |
AzureIoTClient | 6:73793bae15ba | 347 | result = true; |
AzureIoTClient | 6:73793bae15ba | 348 | } |
AzureIoTClient | 6:73793bae15ba | 349 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 350 | } |
AzureIoTClient | 6:73793bae15ba | 351 | return result; |
AzureIoTClient | 6:73793bae15ba | 352 | } |
AzureIoTClient | 6:73793bae15ba | 353 | |
AzureIoTClient | 6:73793bae15ba | 354 | static void destroy_SASObject(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 355 | { |
AzureIoTClient | 6:73793bae15ba | 356 | HTTPAPIEX_SAS_Destroy(handleData->sasObject); |
AzureIoTClient | 6:73793bae15ba | 357 | handleData->sasObject = NULL; |
AzureIoTClient | 6:73793bae15ba | 358 | } |
AzureIoTClient | 6:73793bae15ba | 359 | |
AzureIoTClient | 6:73793bae15ba | 360 | static bool create_deviceSASObject(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 361 | { |
AzureIoTClient | 6:73793bae15ba | 362 | STRING_HANDLE keyName; |
AzureIoTClient | 6:73793bae15ba | 363 | bool result; |
AzureIoTClient | 6:73793bae15ba | 364 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_001: [IoTHubTransportHttp_Create shall invoke URL_EncodeString with an argument of device id.]*/ |
AzureIoTClient | 6:73793bae15ba | 365 | keyName = URL_EncodeString(config->upperConfig->deviceId); |
AzureIoTClient | 6:73793bae15ba | 366 | if (keyName == NULL) |
AzureIoTClient | 6:73793bae15ba | 367 | { |
AzureIoTClient | 6:73793bae15ba | 368 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_002: [If the encode fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 369 | result = false; |
AzureIoTClient | 6:73793bae15ba | 370 | } |
AzureIoTClient | 6:73793bae15ba | 371 | else |
AzureIoTClient | 6:73793bae15ba | 372 | { |
AzureIoTClient | 6:73793bae15ba | 373 | STRING_HANDLE uriResource; |
AzureIoTClient | 6:73793bae15ba | 374 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_003: [IoTHubTransportHttp_Create shall invoke STRING_clone using the previously created hostname.]*/ |
AzureIoTClient | 6:73793bae15ba | 375 | uriResource = STRING_clone(handleData->hostName); |
AzureIoTClient | 6:73793bae15ba | 376 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_004: [If the clone fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 377 | if (uriResource != NULL) |
AzureIoTClient | 6:73793bae15ba | 378 | { |
AzureIoTClient | 6:73793bae15ba | 379 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_005: [IoTHubTransportHttp_Create shall invoke STRING_concat with arguments uriResource and the string "/devices/".]*/ |
AzureIoTClient | 6:73793bae15ba | 380 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_006: [If the concat fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 381 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_007: [IoTHubTransportHttp_Create shall invoke STRING_concat_with_STRING with arguments uriResource and keyName.]*/ |
AzureIoTClient | 6:73793bae15ba | 382 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_008: [If the STRING_concat_with_STRING fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 383 | if ((STRING_concat(uriResource, "/devices/") == 0) && |
AzureIoTClient | 6:73793bae15ba | 384 | (STRING_concat_with_STRING(uriResource, keyName) == 0)) |
AzureIoTClient | 6:73793bae15ba | 385 | { |
AzureIoTClient | 6:73793bae15ba | 386 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_009: [IoTHubTransportHttp_Create shall invoke STRING_construct with an argument of config->upperConfig->deviceKey.]*/ |
AzureIoTClient | 6:73793bae15ba | 387 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_010: [If the STRING_construct fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 388 | STRING_HANDLE key = STRING_construct(config->upperConfig->deviceKey); |
AzureIoTClient | 6:73793bae15ba | 389 | if (key != NULL) |
AzureIoTClient | 6:73793bae15ba | 390 | { |
AzureIoTClient | 6:73793bae15ba | 391 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_013: [The keyName is shortened to zero length, if that fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 392 | if (STRING_empty(keyName) != 0) |
AzureIoTClient | 6:73793bae15ba | 393 | { |
AzureIoTClient | 6:73793bae15ba | 394 | LogError("Unable to form the device key name for the SAS\r\n"); |
AzureIoTClient | 6:73793bae15ba | 395 | result = false; |
AzureIoTClient | 6:73793bae15ba | 396 | } |
AzureIoTClient | 6:73793bae15ba | 397 | else |
AzureIoTClient | 6:73793bae15ba | 398 | { |
AzureIoTClient | 6:73793bae15ba | 399 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_011: [IoTHubTransportHttp_Create shall invoke HTTPAPIEX_SAS_Create with arguments key, uriResource, and zero length keyName.]*/ |
AzureIoTClient | 6:73793bae15ba | 400 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_012: [If the HTTPAPIEX_SAS_Create fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 401 | handleData->sasObject = HTTPAPIEX_SAS_Create(key, uriResource, keyName); |
AzureIoTClient | 6:73793bae15ba | 402 | result = (handleData->sasObject != NULL) ? (true) : (false); |
AzureIoTClient | 6:73793bae15ba | 403 | } |
AzureIoTClient | 6:73793bae15ba | 404 | STRING_delete(key); |
AzureIoTClient | 6:73793bae15ba | 405 | } |
AzureIoTClient | 6:73793bae15ba | 406 | else |
AzureIoTClient | 6:73793bae15ba | 407 | { |
AzureIoTClient | 6:73793bae15ba | 408 | result = false; |
AzureIoTClient | 6:73793bae15ba | 409 | } |
AzureIoTClient | 6:73793bae15ba | 410 | } |
AzureIoTClient | 6:73793bae15ba | 411 | else |
AzureIoTClient | 6:73793bae15ba | 412 | { |
AzureIoTClient | 6:73793bae15ba | 413 | result = false; |
AzureIoTClient | 6:73793bae15ba | 414 | } |
AzureIoTClient | 6:73793bae15ba | 415 | STRING_delete(uriResource); |
AzureIoTClient | 6:73793bae15ba | 416 | } |
AzureIoTClient | 6:73793bae15ba | 417 | else |
AzureIoTClient | 6:73793bae15ba | 418 | { |
AzureIoTClient | 6:73793bae15ba | 419 | result = false; |
AzureIoTClient | 6:73793bae15ba | 420 | } |
AzureIoTClient | 6:73793bae15ba | 421 | STRING_delete(keyName); |
AzureIoTClient | 6:73793bae15ba | 422 | } |
AzureIoTClient | 6:73793bae15ba | 423 | return result; |
AzureIoTClient | 6:73793bae15ba | 424 | } |
AzureIoTClient | 6:73793bae15ba | 425 | |
AzureIoTClient | 6:73793bae15ba | 426 | TRANSPORT_HANDLE IoTHubTransportHttp_Create(const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 427 | { |
AzureIoTClient | 6:73793bae15ba | 428 | HTTPTRANSPORT_HANDLE_DATA* result; |
AzureIoTClient | 6:73793bae15ba | 429 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_001: [If parameter config is NULL then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 430 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_002: [IoTHubTransportHttp_Create shall fail and return NULL if any fields of the config structure are NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 431 | if (config == NULL) |
AzureIoTClient | 6:73793bae15ba | 432 | { |
AzureIoTClient | 6:73793bae15ba | 433 | LogError("invalid arg (configuration is missing)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 434 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 435 | } |
AzureIoTClient | 6:73793bae15ba | 436 | else if (config->upperConfig == NULL) |
AzureIoTClient | 6:73793bae15ba | 437 | { |
AzureIoTClient | 6:73793bae15ba | 438 | LogError("invalid arg (upperConfig is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 439 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 440 | } |
AzureIoTClient | 6:73793bae15ba | 441 | else if (config->waitingToSend == NULL) |
AzureIoTClient | 6:73793bae15ba | 442 | { |
AzureIoTClient | 6:73793bae15ba | 443 | LogError("invalid arg (waitingToSend is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 444 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 445 | } |
AzureIoTClient | 6:73793bae15ba | 446 | else if (config->upperConfig->protocol == NULL) |
AzureIoTClient | 6:73793bae15ba | 447 | { |
AzureIoTClient | 6:73793bae15ba | 448 | LogError("invalid arg (protocol is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 449 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 450 | } |
AzureIoTClient | 6:73793bae15ba | 451 | else if (config->upperConfig->deviceId == NULL) |
AzureIoTClient | 6:73793bae15ba | 452 | { |
AzureIoTClient | 6:73793bae15ba | 453 | LogError("invalid arg (deviceId is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 454 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 455 | } |
AzureIoTClient | 6:73793bae15ba | 456 | else if (config->upperConfig->iotHubName == NULL) |
AzureIoTClient | 6:73793bae15ba | 457 | { |
AzureIoTClient | 6:73793bae15ba | 458 | LogError("invalid arg (iotHubName is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 459 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 460 | } |
AzureIoTClient | 6:73793bae15ba | 461 | else if (config->upperConfig->iotHubSuffix == NULL) |
AzureIoTClient | 6:73793bae15ba | 462 | { |
AzureIoTClient | 6:73793bae15ba | 463 | LogError("invalid arg (iotHubSuffix is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 464 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 465 | } |
AzureIoTClient | 6:73793bae15ba | 466 | else if (config->upperConfig->deviceKey == NULL) |
AzureIoTClient | 6:73793bae15ba | 467 | { |
AzureIoTClient | 6:73793bae15ba | 468 | LogError("invalid arg (deviceKey is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 469 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 470 | } |
AzureIoTClient | 6:73793bae15ba | 471 | else |
AzureIoTClient | 6:73793bae15ba | 472 | { |
AzureIoTClient | 6:73793bae15ba | 473 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 474 | result = (HTTPTRANSPORT_HANDLE_DATA*)malloc(sizeof(HTTPTRANSPORT_HANDLE_DATA)); |
AzureIoTClient | 6:73793bae15ba | 475 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 476 | { |
AzureIoTClient | 6:73793bae15ba | 477 | LogError("unable to malloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 478 | } |
AzureIoTClient | 6:73793bae15ba | 479 | else |
AzureIoTClient | 6:73793bae15ba | 480 | { |
AzureIoTClient | 6:73793bae15ba | 481 | bool was_eventHTTPrelativePath_ok = create_eventHTTPrelativePath(result, config); |
AzureIoTClient | 6:73793bae15ba | 482 | bool was_messageHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_messageHTTPrelativePath(result, config); |
AzureIoTClient | 6:73793bae15ba | 483 | bool was_eventHTTPrequestHeaders_ok = was_messageHTTPrelativePath_ok && create_eventHTTPrequestHeaders(result, config); |
AzureIoTClient | 6:73793bae15ba | 484 | bool was_hostName_ok = was_eventHTTPrequestHeaders_ok && create_hostName(result, config); |
AzureIoTClient | 6:73793bae15ba | 485 | bool was_httpApiExHandle_ok = was_hostName_ok && create_httpApiExHandle(result, config); |
AzureIoTClient | 6:73793bae15ba | 486 | bool was_messageHTTPrequestHeaders_ok = was_httpApiExHandle_ok && create_messageHTTPrequestHeaders(result, config); |
AzureIoTClient | 6:73793bae15ba | 487 | bool was_abandonHTTPrelativePathBegin_ok = was_messageHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, config); |
AzureIoTClient | 6:73793bae15ba | 488 | bool was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, config); |
AzureIoTClient | 6:73793bae15ba | 489 | |
AzureIoTClient | 6:73793bae15ba | 490 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_011: [Otherwise, IoTHubTransportHttp_Create shall set a flag called "DoWork_PullMessage" to false, succeed and return a non-NULL value.]*/ |
AzureIoTClient | 6:73793bae15ba | 491 | if (was_sasObject_ok) |
AzureIoTClient | 6:73793bae15ba | 492 | { |
AzureIoTClient | 6:73793bae15ba | 493 | result->DoWork_PullMessage = false; |
AzureIoTClient | 6:73793bae15ba | 494 | result->doBatchedTransfers = false; |
AzureIoTClient | 6:73793bae15ba | 495 | result->isFirstPoll = true; |
AzureIoTClient | 6:73793bae15ba | 496 | result->getMinimumPollingTime = DEFAULT_GETMINIMUMPOLLINGTIME; |
AzureIoTClient | 6:73793bae15ba | 497 | result->waitingToSend = config->waitingToSend; |
AzureIoTClient | 6:73793bae15ba | 498 | DList_InitializeListHead(&(result->eventConfirmations)); |
AzureIoTClient | 6:73793bae15ba | 499 | } |
AzureIoTClient | 6:73793bae15ba | 500 | else |
AzureIoTClient | 6:73793bae15ba | 501 | { |
AzureIoTClient | 6:73793bae15ba | 502 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_004: [If creating the string fail for any reason then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 503 | if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result); |
AzureIoTClient | 6:73793bae15ba | 504 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_035: [If creating the message HTTP relative path fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 505 | if (was_messageHTTPrelativePath_ok) destroy_messageHTTPrelativePath(result); |
AzureIoTClient | 6:73793bae15ba | 506 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_006: [If creating the event HTTP request headers fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 507 | if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result); |
AzureIoTClient | 6:73793bae15ba | 508 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_008: [If creating the hostname fails then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 509 | if (was_hostName_ok) destroy_hostName(result); |
AzureIoTClient | 6:73793bae15ba | 510 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_010: [If creating the HTTPAPIEX_HANDLE fails then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 511 | if (was_httpApiExHandle_ok) destroy_httpApiExHandle(result); |
AzureIoTClient | 6:73793bae15ba | 512 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_060: [If creating message HTTP request headers then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 513 | if (was_messageHTTPrequestHeaders_ok) destroy_messageHTTPrequestHeaders(result); |
AzureIoTClient | 6:73793bae15ba | 514 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_062: [If creating the abandonHTTPrelativePathBegin fails then IoTHubTransportHttp_Create shall fail and return NULL] */ |
AzureIoTClient | 6:73793bae15ba | 515 | if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result); |
AzureIoTClient | 6:73793bae15ba | 516 | |
AzureIoTClient | 6:73793bae15ba | 517 | free(result); |
AzureIoTClient | 6:73793bae15ba | 518 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 519 | } |
AzureIoTClient | 6:73793bae15ba | 520 | } |
AzureIoTClient | 6:73793bae15ba | 521 | } |
AzureIoTClient | 6:73793bae15ba | 522 | return result; |
AzureIoTClient | 6:73793bae15ba | 523 | } |
AzureIoTClient | 6:73793bae15ba | 524 | |
AzureIoTClient | 6:73793bae15ba | 525 | void IoTHubTransportHttp_Destroy(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 526 | { |
AzureIoTClient | 6:73793bae15ba | 527 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_012: [IoTHubTransportHttp_Destroy shall do nothing if parameter handle is NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 528 | if (handle != NULL) |
AzureIoTClient | 6:73793bae15ba | 529 | { |
AzureIoTClient | 6:73793bae15ba | 530 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_013: [Otherwise IoTHubTransportHttp_Destroy shall free all the resources currently in use.] */ |
AzureIoTClient | 6:73793bae15ba | 531 | destroy_eventHTTPrelativePath(handle); |
AzureIoTClient | 6:73793bae15ba | 532 | destroy_messageHTTPrelativePath(handle); |
AzureIoTClient | 6:73793bae15ba | 533 | destroy_eventHTTPrequestHeaders(handle); |
AzureIoTClient | 6:73793bae15ba | 534 | destroy_hostName(handle); |
AzureIoTClient | 6:73793bae15ba | 535 | destroy_httpApiExHandle(handle); |
AzureIoTClient | 6:73793bae15ba | 536 | destroy_messageHTTPrequestHeaders(handle); |
AzureIoTClient | 6:73793bae15ba | 537 | destroy_abandonHTTPrelativePathBegin(handle); |
AzureIoTClient | 6:73793bae15ba | 538 | destroy_SASObject(handle); |
AzureIoTClient | 6:73793bae15ba | 539 | free(handle); |
AzureIoTClient | 6:73793bae15ba | 540 | } |
AzureIoTClient | 6:73793bae15ba | 541 | } |
AzureIoTClient | 6:73793bae15ba | 542 | |
AzureIoTClient | 6:73793bae15ba | 543 | int IoTHubTransportHttp_Subscribe(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 544 | { |
AzureIoTClient | 6:73793bae15ba | 545 | int result; |
AzureIoTClient | 6:73793bae15ba | 546 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_014: [If parameter handle is NULL then IoTHubTransportHttp_Subscribe shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:73793bae15ba | 547 | if (handle == NULL) |
AzureIoTClient | 6:73793bae15ba | 548 | { |
AzureIoTClient | 6:73793bae15ba | 549 | LogError("invalid arg passed to IoTHubTransportHttp_Subscribe\r\n"); |
AzureIoTClient | 6:73793bae15ba | 550 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 551 | } |
AzureIoTClient | 6:73793bae15ba | 552 | else |
AzureIoTClient | 6:73793bae15ba | 553 | { |
AzureIoTClient | 6:73793bae15ba | 554 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_056: [Otherwise, IoTHubTransportHttp_Subscribe shall set the flag called DoWork_PullMessages to true and succeed.] */ |
AzureIoTClient | 6:73793bae15ba | 555 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 556 | handleData->DoWork_PullMessage = true; |
AzureIoTClient | 6:73793bae15ba | 557 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 558 | } |
AzureIoTClient | 6:73793bae15ba | 559 | return result; |
AzureIoTClient | 6:73793bae15ba | 560 | } |
AzureIoTClient | 6:73793bae15ba | 561 | |
AzureIoTClient | 6:73793bae15ba | 562 | void IoTHubTransportHttp_Unsubscribe(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 563 | { |
AzureIoTClient | 6:73793bae15ba | 564 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_016: [If parameter handle is NULL then IoTHubTransportHttp_Unsubscribe shall do nothing.] */ |
AzureIoTClient | 6:73793bae15ba | 565 | if (handle != NULL) |
AzureIoTClient | 6:73793bae15ba | 566 | { |
AzureIoTClient | 6:73793bae15ba | 567 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 568 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_058: [Otherwise it shall set the flag DoWork_PullMessage to false.] */ |
AzureIoTClient | 6:73793bae15ba | 569 | handleData->DoWork_PullMessage = false; |
AzureIoTClient | 6:73793bae15ba | 570 | } |
AzureIoTClient | 6:73793bae15ba | 571 | } |
AzureIoTClient | 6:73793bae15ba | 572 | |
AzureIoTClient | 6:73793bae15ba | 573 | /*produces a representation of the properties, if they exist*/ |
AzureIoTClient | 6:73793bae15ba | 574 | /*if they do not exist, produces ""*/ |
AzureIoTClient | 6:73793bae15ba | 575 | static int concat_Properties(STRING_HANDLE existing, MAP_HANDLE map, size_t* propertiesMessageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 576 | { |
AzureIoTClient | 6:73793bae15ba | 577 | int result; |
AzureIoTClient | 6:73793bae15ba | 578 | const char*const* keys; |
AzureIoTClient | 6:73793bae15ba | 579 | const char*const* values; |
AzureIoTClient | 6:73793bae15ba | 580 | size_t count; |
AzureIoTClient | 6:73793bae15ba | 581 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
AzureIoTClient | 6:73793bae15ba | 582 | { |
AzureIoTClient | 6:73793bae15ba | 583 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 584 | LogError("error while Map_GetInternals\r\n"); |
AzureIoTClient | 6:73793bae15ba | 585 | } |
AzureIoTClient | 6:73793bae15ba | 586 | else |
AzureIoTClient | 6:73793bae15ba | 587 | { |
AzureIoTClient | 6:73793bae15ba | 588 | |
AzureIoTClient | 6:73793bae15ba | 589 | if (count == 0) |
AzureIoTClient | 6:73793bae15ba | 590 | { |
AzureIoTClient | 6:73793bae15ba | 591 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_076: [If IoTHubMessage does not have properties, then "properties":{...} shall be missing from the payload*/ |
AzureIoTClient | 6:73793bae15ba | 592 | /*no properties - do nothing with existing*/ |
AzureIoTClient | 6:73793bae15ba | 593 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 594 | *propertiesMessageSizeContribution = 0; |
AzureIoTClient | 6:73793bae15ba | 595 | } |
AzureIoTClient | 6:73793bae15ba | 596 | else |
AzureIoTClient | 6:73793bae15ba | 597 | { |
AzureIoTClient | 6:73793bae15ba | 598 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_075: [If IoTHubMessage has properties, then they shall be serialized at the same level as "body" using the following pattern: "properties":{"iothub-app-name1":"value1","iothub-app-name2":"value2*/ |
AzureIoTClient | 6:73793bae15ba | 599 | if (STRING_concat(existing, ",\"properties\":") != 0) |
AzureIoTClient | 6:73793bae15ba | 600 | { |
AzureIoTClient | 6:73793bae15ba | 601 | /*go ahead and return it*/ |
AzureIoTClient | 6:73793bae15ba | 602 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 603 | LogError("failed STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 604 | } |
AzureIoTClient | 6:73793bae15ba | 605 | else if (appendMapToJSON(existing, keys, values, count) != 0) |
AzureIoTClient | 6:73793bae15ba | 606 | { |
AzureIoTClient | 6:73793bae15ba | 607 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 608 | LogError("unable to append the properties\r\n"); |
AzureIoTClient | 6:73793bae15ba | 609 | } |
AzureIoTClient | 6:73793bae15ba | 610 | else |
AzureIoTClient | 6:73793bae15ba | 611 | { |
AzureIoTClient | 6:73793bae15ba | 612 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 613 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 614 | *propertiesMessageSizeContribution = 0; |
AzureIoTClient | 6:73793bae15ba | 615 | for (i = 0;i < count;i++) |
AzureIoTClient | 6:73793bae15ba | 616 | { |
AzureIoTClient | 6:73793bae15ba | 617 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_120: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 618 | *propertiesMessageSizeContribution += (strlen(keys[i]) + strlen(values[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
AzureIoTClient | 6:73793bae15ba | 619 | } |
AzureIoTClient | 6:73793bae15ba | 620 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 621 | } |
AzureIoTClient | 6:73793bae15ba | 622 | } |
AzureIoTClient | 6:73793bae15ba | 623 | } |
AzureIoTClient | 6:73793bae15ba | 624 | return result; |
AzureIoTClient | 6:73793bae15ba | 625 | } |
AzureIoTClient | 6:73793bae15ba | 626 | |
AzureIoTClient | 6:73793bae15ba | 627 | /*produces a JSON representation of the map : {"a": "value_of_a","b":"value_of_b"}*/ |
AzureIoTClient | 6:73793bae15ba | 628 | static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count) /*under consideration: move to MAP module when it has more than 1 user*/ |
AzureIoTClient | 6:73793bae15ba | 629 | { |
AzureIoTClient | 6:73793bae15ba | 630 | int result; |
AzureIoTClient | 6:73793bae15ba | 631 | if (STRING_concat(existing, "{") != 0) |
AzureIoTClient | 6:73793bae15ba | 632 | { |
AzureIoTClient | 6:73793bae15ba | 633 | /*go on and return it*/ |
AzureIoTClient | 6:73793bae15ba | 634 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 635 | LogError("STRING_construct failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 636 | } |
AzureIoTClient | 6:73793bae15ba | 637 | else |
AzureIoTClient | 6:73793bae15ba | 638 | { |
AzureIoTClient | 6:73793bae15ba | 639 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 640 | for (i = 0; i < count; i++) |
AzureIoTClient | 6:73793bae15ba | 641 | { |
AzureIoTClient | 6:73793bae15ba | 642 | if (!( |
AzureIoTClient | 6:73793bae15ba | 643 | (STRING_concat(existing, (i == 0) ? "\"" IOTHUB_APP_PREFIX : ",\"" IOTHUB_APP_PREFIX) == 0) && |
AzureIoTClient | 6:73793bae15ba | 644 | (STRING_concat(existing, keys[i]) == 0) && |
AzureIoTClient | 6:73793bae15ba | 645 | (STRING_concat(existing, "\":\"") == 0) && |
AzureIoTClient | 6:73793bae15ba | 646 | (STRING_concat(existing, values[i]) == 0) && |
AzureIoTClient | 6:73793bae15ba | 647 | (STRING_concat(existing, "\"") == 0) |
AzureIoTClient | 6:73793bae15ba | 648 | )) |
AzureIoTClient | 6:73793bae15ba | 649 | { |
AzureIoTClient | 6:73793bae15ba | 650 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 651 | break; |
AzureIoTClient | 6:73793bae15ba | 652 | } |
AzureIoTClient | 6:73793bae15ba | 653 | } |
AzureIoTClient | 6:73793bae15ba | 654 | |
AzureIoTClient | 6:73793bae15ba | 655 | if (i < count) |
AzureIoTClient | 6:73793bae15ba | 656 | { |
AzureIoTClient | 6:73793bae15ba | 657 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 658 | /*error, let it go through*/ |
AzureIoTClient | 6:73793bae15ba | 659 | } |
AzureIoTClient | 6:73793bae15ba | 660 | else if (STRING_concat(existing, "}") != 0) |
AzureIoTClient | 6:73793bae15ba | 661 | { |
AzureIoTClient | 6:73793bae15ba | 662 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 663 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 664 | } |
AzureIoTClient | 6:73793bae15ba | 665 | else |
AzureIoTClient | 6:73793bae15ba | 666 | { |
AzureIoTClient | 6:73793bae15ba | 667 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 668 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 669 | } |
AzureIoTClient | 6:73793bae15ba | 670 | } |
AzureIoTClient | 6:73793bae15ba | 671 | return result; |
AzureIoTClient | 6:73793bae15ba | 672 | } |
AzureIoTClient | 6:73793bae15ba | 673 | |
AzureIoTClient | 6:73793bae15ba | 674 | /*makes the following string:{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
AzureIoTClient | 6:73793bae15ba | 675 | /*return NULL if there was a failure, or a non-NULL STRING_HANDLE that contains the intended data*/ |
AzureIoTClient | 6:73793bae15ba | 676 | static STRING_HANDLE make1EventJSONitem(PDLIST_ENTRY item, size_t *messageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 677 | { |
AzureIoTClient | 6:73793bae15ba | 678 | STRING_HANDLE result; /*temp wants to contain :{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
AzureIoTClient | 6:73793bae15ba | 679 | IOTHUB_MESSAGE_LIST* message = containingRecord(item, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 6:73793bae15ba | 680 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 681 | |
AzureIoTClient | 6:73793bae15ba | 682 | switch (contentType) |
AzureIoTClient | 6:73793bae15ba | 683 | { |
AzureIoTClient | 6:73793bae15ba | 684 | case IOTHUBMESSAGE_BYTEARRAY: |
AzureIoTClient | 6:73793bae15ba | 685 | { |
AzureIoTClient | 6:73793bae15ba | 686 | result = STRING_construct("{\"body\":\""); |
AzureIoTClient | 6:73793bae15ba | 687 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 688 | { |
AzureIoTClient | 6:73793bae15ba | 689 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 690 | } |
AzureIoTClient | 6:73793bae15ba | 691 | else |
AzureIoTClient | 6:73793bae15ba | 692 | { |
AzureIoTClient | 6:73793bae15ba | 693 | const unsigned char* source; |
AzureIoTClient | 6:73793bae15ba | 694 | size_t size; |
AzureIoTClient | 6:73793bae15ba | 695 | |
AzureIoTClient | 6:73793bae15ba | 696 | if (IoTHubMessage_GetByteArray(message->messageHandle, &source, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 6:73793bae15ba | 697 | { |
AzureIoTClient | 6:73793bae15ba | 698 | LogError("unable to get the data for the message.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 699 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 700 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 701 | } |
AzureIoTClient | 6:73793bae15ba | 702 | else |
AzureIoTClient | 6:73793bae15ba | 703 | { |
AzureIoTClient | 6:73793bae15ba | 704 | STRING_HANDLE encoded = Base64_Encode_Bytes(source, size); |
AzureIoTClient | 6:73793bae15ba | 705 | if (encoded == NULL) |
AzureIoTClient | 6:73793bae15ba | 706 | { |
AzureIoTClient | 6:73793bae15ba | 707 | LogError("unable to Base64_Encode_Bytes.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 708 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 709 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 710 | } |
AzureIoTClient | 6:73793bae15ba | 711 | else |
AzureIoTClient | 6:73793bae15ba | 712 | { |
AzureIoTClient | 6:73793bae15ba | 713 | size_t propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 714 | if (!( |
AzureIoTClient | 6:73793bae15ba | 715 | (STRING_concat_with_STRING(result, encoded) == 0) && |
AzureIoTClient | 6:73793bae15ba | 716 | (STRING_concat(result, "\"") == 0) && /*\" because closing value*/ |
AzureIoTClient | 6:73793bae15ba | 717 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
AzureIoTClient | 6:73793bae15ba | 718 | (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/ |
AzureIoTClient | 6:73793bae15ba | 719 | )) |
AzureIoTClient | 6:73793bae15ba | 720 | { |
AzureIoTClient | 6:73793bae15ba | 721 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 722 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 723 | LogError("unable to STRING_concat_with_STRING.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 724 | } |
AzureIoTClient | 6:73793bae15ba | 725 | else |
AzureIoTClient | 6:73793bae15ba | 726 | { |
AzureIoTClient | 6:73793bae15ba | 727 | /*all is fine... */ |
AzureIoTClient | 6:73793bae15ba | 728 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_119: [The message size is computed from the length of the payload + 384.] */ |
AzureIoTClient | 6:73793bae15ba | 729 | *messageSizeContribution = size + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 730 | } |
AzureIoTClient | 6:73793bae15ba | 731 | STRING_delete(encoded); |
AzureIoTClient | 6:73793bae15ba | 732 | } |
AzureIoTClient | 6:73793bae15ba | 733 | } |
AzureIoTClient | 6:73793bae15ba | 734 | } |
AzureIoTClient | 6:73793bae15ba | 735 | break; |
AzureIoTClient | 6:73793bae15ba | 736 | } |
AzureIoTClient | 6:73793bae15ba | 737 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_113: [If a messages to be send has type IOTHUBMESSAGE_STRING, then its serialization shall be {"body":"JSON encoding of the string", "base64Encoded":false}] */ |
AzureIoTClient | 6:73793bae15ba | 738 | case IOTHUBMESSAGE_STRING: |
AzureIoTClient | 6:73793bae15ba | 739 | { |
AzureIoTClient | 6:73793bae15ba | 740 | result = STRING_construct("{\"body\":"); |
AzureIoTClient | 6:73793bae15ba | 741 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 742 | { |
AzureIoTClient | 6:73793bae15ba | 743 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 744 | } |
AzureIoTClient | 6:73793bae15ba | 745 | else |
AzureIoTClient | 6:73793bae15ba | 746 | { |
AzureIoTClient | 6:73793bae15ba | 747 | const char* source = IoTHubMessage_GetString(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 748 | if (source == NULL) |
AzureIoTClient | 6:73793bae15ba | 749 | { |
AzureIoTClient | 6:73793bae15ba | 750 | LogError("unable to IoTHubMessage_GetString\r\n"); |
AzureIoTClient | 6:73793bae15ba | 751 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 752 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 753 | } |
AzureIoTClient | 6:73793bae15ba | 754 | else |
AzureIoTClient | 6:73793bae15ba | 755 | { |
AzureIoTClient | 6:73793bae15ba | 756 | STRING_HANDLE asJson = STRING_new_JSON(source); |
AzureIoTClient | 6:73793bae15ba | 757 | if (asJson == NULL) |
AzureIoTClient | 6:73793bae15ba | 758 | { |
AzureIoTClient | 6:73793bae15ba | 759 | LogError("unable to STRING_new_JSON\r\n"); |
AzureIoTClient | 6:73793bae15ba | 760 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 761 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 762 | } |
AzureIoTClient | 6:73793bae15ba | 763 | else |
AzureIoTClient | 6:73793bae15ba | 764 | { |
AzureIoTClient | 6:73793bae15ba | 765 | size_t propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 766 | if (!( |
AzureIoTClient | 6:73793bae15ba | 767 | (STRING_concat_with_STRING(result, asJson) == 0) && |
AzureIoTClient | 6:73793bae15ba | 768 | (STRING_concat(result, ",\"base64Encoded\":false") == 0) && |
AzureIoTClient | 6:73793bae15ba | 769 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
AzureIoTClient | 6:73793bae15ba | 770 | (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/ |
AzureIoTClient | 6:73793bae15ba | 771 | )) |
AzureIoTClient | 6:73793bae15ba | 772 | { |
AzureIoTClient | 6:73793bae15ba | 773 | LogError("unable to STRING_concat_with_STRING"); |
AzureIoTClient | 6:73793bae15ba | 774 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 775 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 776 | } |
AzureIoTClient | 6:73793bae15ba | 777 | else |
AzureIoTClient | 6:73793bae15ba | 778 | { |
AzureIoTClient | 6:73793bae15ba | 779 | /*result has the intended content*/ |
AzureIoTClient | 6:73793bae15ba | 780 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_119: [The message size is computed from the length of the payload + 384.] */ |
AzureIoTClient | 6:73793bae15ba | 781 | *messageSizeContribution = strlen(source) + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 782 | } |
AzureIoTClient | 6:73793bae15ba | 783 | STRING_delete(asJson); |
AzureIoTClient | 6:73793bae15ba | 784 | } |
AzureIoTClient | 6:73793bae15ba | 785 | } |
AzureIoTClient | 6:73793bae15ba | 786 | } |
AzureIoTClient | 6:73793bae15ba | 787 | break; |
AzureIoTClient | 6:73793bae15ba | 788 | } |
AzureIoTClient | 6:73793bae15ba | 789 | default: |
AzureIoTClient | 6:73793bae15ba | 790 | { |
AzureIoTClient | 6:73793bae15ba | 791 | LogError("an unknown message type was encountered (%d)\r\n", contentType); |
AzureIoTClient | 6:73793bae15ba | 792 | result = NULL; /*unknown message type*/ |
AzureIoTClient | 6:73793bae15ba | 793 | break; |
AzureIoTClient | 6:73793bae15ba | 794 | } |
AzureIoTClient | 6:73793bae15ba | 795 | } |
AzureIoTClient | 6:73793bae15ba | 796 | return result; |
AzureIoTClient | 6:73793bae15ba | 797 | } |
AzureIoTClient | 6:73793bae15ba | 798 | |
AzureIoTClient | 6:73793bae15ba | 799 | #define MAKE_PAYLOAD_RESULT_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 800 | MAKE_PAYLOAD_OK, /*returned when there is a payload to be later send by HTTP*/ \ |
AzureIoTClient | 6:73793bae15ba | 801 | MAKE_PAYLOAD_NO_ITEMS, /*returned when there are no items to be send*/ \ |
AzureIoTClient | 6:73793bae15ba | 802 | MAKE_PAYLOAD_ERROR, /*returned when there were errors*/ \ |
AzureIoTClient | 6:73793bae15ba | 803 | MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT /*returned when the first item doesn't fit*/ |
AzureIoTClient | 6:73793bae15ba | 804 | |
AzureIoTClient | 6:73793bae15ba | 805 | DEFINE_ENUM(MAKE_PAYLOAD_RESULT, MAKE_PAYLOAD_RESULT_VALUES); |
AzureIoTClient | 6:73793bae15ba | 806 | |
AzureIoTClient | 6:73793bae15ba | 807 | /*this function assembles several {"body":"base64 encoding of the message content"," base64Encoded": true} into 1 payload*/ |
AzureIoTClient | 6:73793bae15ba | 808 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_070: [IoTHubTransportHttp_DoWork shall build the following string:[{"body":"base64 encoding of the message1 content"},{"body":"base64 encoding of the message2 content"}...]]*/ |
AzureIoTClient | 6:73793bae15ba | 809 | static MAKE_PAYLOAD_RESULT makePayload(HTTPTRANSPORT_HANDLE_DATA* handleData, STRING_HANDLE* payload) |
AzureIoTClient | 6:73793bae15ba | 810 | { |
AzureIoTClient | 6:73793bae15ba | 811 | MAKE_PAYLOAD_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 812 | size_t allMessagesSize = 0; |
AzureIoTClient | 6:73793bae15ba | 813 | *payload = STRING_construct("["); |
AzureIoTClient | 6:73793bae15ba | 814 | if (*payload == NULL) |
AzureIoTClient | 6:73793bae15ba | 815 | { |
AzureIoTClient | 6:73793bae15ba | 816 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 817 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 818 | } |
AzureIoTClient | 6:73793bae15ba | 819 | else |
AzureIoTClient | 6:73793bae15ba | 820 | { |
AzureIoTClient | 6:73793bae15ba | 821 | bool isFirst = true; |
AzureIoTClient | 6:73793bae15ba | 822 | PDLIST_ENTRY actual; |
AzureIoTClient | 10:b012891d0a54 | 823 | bool keepGoing = true; /*keepGoing gets sometimes to false from within the loop*/ |
AzureIoTClient | 6:73793bae15ba | 824 | /*either all the items enter the list or only some*/ |
AzureIoTClient | 6:73793bae15ba | 825 | result = MAKE_PAYLOAD_OK; /*optimistically initializing it*/ |
AzureIoTClient | 6:73793bae15ba | 826 | while (keepGoing && ((actual = handleData->waitingToSend->Flink) != handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 827 | { |
AzureIoTClient | 6:73793bae15ba | 828 | size_t messageSize; |
AzureIoTClient | 6:73793bae15ba | 829 | STRING_HANDLE temp = make1EventJSONitem(actual, &messageSize); |
AzureIoTClient | 6:73793bae15ba | 830 | if (isFirst) |
AzureIoTClient | 6:73793bae15ba | 831 | { |
AzureIoTClient | 6:73793bae15ba | 832 | isFirst = false; |
AzureIoTClient | 6:73793bae15ba | 833 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 834 | if (temp == NULL) /*first item failed to create, nothing to send*/ |
AzureIoTClient | 6:73793bae15ba | 835 | { |
AzureIoTClient | 6:73793bae15ba | 836 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 837 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 838 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 839 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 840 | } |
AzureIoTClient | 6:73793bae15ba | 841 | else |
AzureIoTClient | 6:73793bae15ba | 842 | { |
AzureIoTClient | 6:73793bae15ba | 843 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_071: [If the oldest message in waitingToSend causes the message size to exceed the message size limit then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.]*/ |
AzureIoTClient | 6:73793bae15ba | 844 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_118: [The message size shall be limited to 255KB - 1 byte.]*/ |
AzureIoTClient | 6:73793bae15ba | 845 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 846 | { |
AzureIoTClient | 6:73793bae15ba | 847 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 848 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 849 | result = MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT; |
AzureIoTClient | 6:73793bae15ba | 850 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 851 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 852 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 853 | } |
AzureIoTClient | 6:73793bae15ba | 854 | else |
AzureIoTClient | 6:73793bae15ba | 855 | { |
AzureIoTClient | 6:73793bae15ba | 856 | if (STRING_concat_with_STRING(*payload, temp) != 0) |
AzureIoTClient | 6:73793bae15ba | 857 | { |
AzureIoTClient | 6:73793bae15ba | 858 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 859 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 860 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 861 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 862 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 863 | } |
AzureIoTClient | 6:73793bae15ba | 864 | else |
AzureIoTClient | 6:73793bae15ba | 865 | { |
AzureIoTClient | 6:73793bae15ba | 866 | /*first item was put nicely in the payload*/ |
AzureIoTClient | 6:73793bae15ba | 867 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 868 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 869 | allMessagesSize += messageSize; |
AzureIoTClient | 6:73793bae15ba | 870 | } |
AzureIoTClient | 6:73793bae15ba | 871 | } |
AzureIoTClient | 6:73793bae15ba | 872 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 873 | } |
AzureIoTClient | 6:73793bae15ba | 874 | } |
AzureIoTClient | 6:73793bae15ba | 875 | else |
AzureIoTClient | 6:73793bae15ba | 876 | { |
AzureIoTClient | 6:73793bae15ba | 877 | /*there is at least 1 item already in the payload*/ |
AzureIoTClient | 6:73793bae15ba | 878 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 879 | { |
AzureIoTClient | 6:73793bae15ba | 880 | /*there are multiple payloads encoded, the last one had an internal error, just go with those - closing the payload happens "after the loop"*/ |
AzureIoTClient | 6:73793bae15ba | 881 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 882 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 883 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 884 | } |
AzureIoTClient | 6:73793bae15ba | 885 | else |
AzureIoTClient | 6:73793bae15ba | 886 | { |
AzureIoTClient | 6:73793bae15ba | 887 | if (allMessagesSize + messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 888 | { |
AzureIoTClient | 6:73793bae15ba | 889 | /*this item doesn't make it to the payload, but the payload is valid so far*/ |
AzureIoTClient | 6:73793bae15ba | 890 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 891 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 892 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 893 | } |
AzureIoTClient | 6:73793bae15ba | 894 | else if (STRING_concat_with_STRING(*payload, temp) != 0) |
AzureIoTClient | 6:73793bae15ba | 895 | { |
AzureIoTClient | 6:73793bae15ba | 896 | /*should still send what there is so far...*/ |
AzureIoTClient | 6:73793bae15ba | 897 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 898 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 899 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 900 | } |
AzureIoTClient | 6:73793bae15ba | 901 | else |
AzureIoTClient | 6:73793bae15ba | 902 | { |
AzureIoTClient | 6:73793bae15ba | 903 | /*cool, the payload made it there, let's continue... */ |
AzureIoTClient | 6:73793bae15ba | 904 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 905 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 906 | allMessagesSize += messageSize; |
AzureIoTClient | 6:73793bae15ba | 907 | } |
AzureIoTClient | 6:73793bae15ba | 908 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 909 | } |
AzureIoTClient | 6:73793bae15ba | 910 | } |
AzureIoTClient | 6:73793bae15ba | 911 | } |
AzureIoTClient | 6:73793bae15ba | 912 | |
AzureIoTClient | 6:73793bae15ba | 913 | /*closing the payload*/ |
AzureIoTClient | 6:73793bae15ba | 914 | if (result == MAKE_PAYLOAD_OK) |
AzureIoTClient | 6:73793bae15ba | 915 | { |
AzureIoTClient | 6:73793bae15ba | 916 | ((char*)STRING_c_str(*payload))[STRING_length(*payload) - 1] = ']'; /*TODO - do this in STRING_HANDLE*/ |
AzureIoTClient | 6:73793bae15ba | 917 | } |
AzureIoTClient | 6:73793bae15ba | 918 | else |
AzureIoTClient | 6:73793bae15ba | 919 | { |
AzureIoTClient | 6:73793bae15ba | 920 | /*no need to close anything*/ |
AzureIoTClient | 6:73793bae15ba | 921 | } |
AzureIoTClient | 6:73793bae15ba | 922 | } |
AzureIoTClient | 6:73793bae15ba | 923 | return result; |
AzureIoTClient | 6:73793bae15ba | 924 | } |
AzureIoTClient | 6:73793bae15ba | 925 | |
AzureIoTClient | 6:73793bae15ba | 926 | static void reversePutListBackIn(PDLIST_ENTRY source, PDLIST_ENTRY destination) |
AzureIoTClient | 6:73793bae15ba | 927 | { |
AzureIoTClient | 6:73793bae15ba | 928 | /*this function takes a list, and inserts it in another list. When done in the context of this file, it reverses the effects of a not-able-to-send situation*/ |
AzureIoTClient | 6:73793bae15ba | 929 | DList_AppendTailList(destination->Flink, source); |
AzureIoTClient | 6:73793bae15ba | 930 | DList_RemoveEntryList(source); |
AzureIoTClient | 6:73793bae15ba | 931 | DList_InitializeListHead(source); |
AzureIoTClient | 6:73793bae15ba | 932 | } |
AzureIoTClient | 6:73793bae15ba | 933 | |
AzureIoTClient | 6:73793bae15ba | 934 | static void DoEvent(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 935 | { |
AzureIoTClient | 6:73793bae15ba | 936 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 937 | if (DList_IsListEmpty(handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 938 | { |
AzureIoTClient | 6:73793bae15ba | 939 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_019: [If the list is empty then IoTHubTransportHttp_DoWork shall proceed to the following action.] */ |
AzureIoTClient | 6:73793bae15ba | 940 | } |
AzureIoTClient | 6:73793bae15ba | 941 | else |
AzureIoTClient | 6:73793bae15ba | 942 | { |
AzureIoTClient | 6:73793bae15ba | 943 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_101: [If option SetBatching is true then _Dowork shall send batched event message as specced below.] */ |
AzureIoTClient | 6:73793bae15ba | 944 | if (handleData->doBatchedTransfers) |
AzureIoTClient | 6:73793bae15ba | 945 | { |
AzureIoTClient | 6:73793bae15ba | 946 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_102: [Request HTTP headers shall have the value of "Content-Type" created or updated to "application/vnd.microsoft.iothub.json" by a call to HTTPHeaders_ReplaceHeaderNameValuePair.] */ |
AzureIoTClient | 6:73793bae15ba | 947 | if (HTTPHeaders_ReplaceHeaderNameValuePair(handleData->eventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_VND_MICROSOFT_IOTHUB_JSON) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 948 | { |
AzureIoTClient | 6:73793bae15ba | 949 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_103: [If updating Content-Type fails for any reason, then _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 950 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 951 | } |
AzureIoTClient | 6:73793bae15ba | 952 | else |
AzureIoTClient | 6:73793bae15ba | 953 | { |
AzureIoTClient | 6:73793bae15ba | 954 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_018: [It shall inspect the "waitingToSend" DLIST passed in config structure.] */ |
AzureIoTClient | 6:73793bae15ba | 955 | STRING_HANDLE payload; |
AzureIoTClient | 6:73793bae15ba | 956 | switch (makePayload(handleData, &payload)) |
AzureIoTClient | 6:73793bae15ba | 957 | { |
AzureIoTClient | 6:73793bae15ba | 958 | case MAKE_PAYLOAD_OK: |
AzureIoTClient | 6:73793bae15ba | 959 | { |
AzureIoTClient | 6:73793bae15ba | 960 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_031: [Once a final payload has been obtained, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters:] */ |
AzureIoTClient | 6:73793bae15ba | 961 | BUFFER_HANDLE temp = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 962 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 963 | { |
AzureIoTClient | 6:73793bae15ba | 964 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 965 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 966 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 967 | } |
AzureIoTClient | 6:73793bae15ba | 968 | else |
AzureIoTClient | 6:73793bae15ba | 969 | { |
AzureIoTClient | 6:73793bae15ba | 970 | if (BUFFER_build(temp, (const unsigned char*)STRING_c_str(payload), STRING_length(payload)) != 0) |
AzureIoTClient | 6:73793bae15ba | 971 | { |
AzureIoTClient | 6:73793bae15ba | 972 | LogError("unable to BUFFER_build\r\n"); |
AzureIoTClient | 6:73793bae15ba | 973 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 974 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 975 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 976 | } |
AzureIoTClient | 6:73793bae15ba | 977 | else |
AzureIoTClient | 6:73793bae15ba | 978 | { |
AzureIoTClient | 6:73793bae15ba | 979 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 980 | HTTPAPIEX_RESULT r; |
AzureIoTClient | 6:73793bae15ba | 981 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 982 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 983 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 984 | HTTPAPI_REQUEST_POST, |
AzureIoTClient | 6:73793bae15ba | 985 | STRING_c_str(handleData->eventHTTPrelativePath), |
AzureIoTClient | 6:73793bae15ba | 986 | handleData->eventHTTPrequestHeaders, |
AzureIoTClient | 6:73793bae15ba | 987 | temp, |
AzureIoTClient | 6:73793bae15ba | 988 | &statusCode, |
AzureIoTClient | 6:73793bae15ba | 989 | NULL, |
AzureIoTClient | 6:73793bae15ba | 990 | NULL |
AzureIoTClient | 6:73793bae15ba | 991 | )) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 992 | { |
AzureIoTClient | 6:73793bae15ba | 993 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 994 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 995 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 996 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 997 | } |
AzureIoTClient | 6:73793bae15ba | 998 | else |
AzureIoTClient | 6:73793bae15ba | 999 | { |
AzureIoTClient | 6:73793bae15ba | 1000 | if (statusCode < 300) |
AzureIoTClient | 6:73793bae15ba | 1001 | { |
AzureIoTClient | 6:73793bae15ba | 1002 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_067: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list containing all the items batched, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCESS. The batched items shall be removed from waitingToSend.] */ |
AzureIoTClient | 6:73793bae15ba | 1003 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); |
AzureIoTClient | 6:73793bae15ba | 1004 | } |
AzureIoTClient | 6:73793bae15ba | 1005 | else |
AzureIoTClient | 6:73793bae15ba | 1006 | { |
AzureIoTClient | 6:73793bae15ba | 1007 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 1008 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 1009 | LogError("unexpected HTTP status code (%u)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1010 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 1011 | } |
AzureIoTClient | 6:73793bae15ba | 1012 | } |
AzureIoTClient | 6:73793bae15ba | 1013 | } |
AzureIoTClient | 6:73793bae15ba | 1014 | BUFFER_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 1015 | } |
AzureIoTClient | 6:73793bae15ba | 1016 | STRING_delete(payload); |
AzureIoTClient | 6:73793bae15ba | 1017 | break; |
AzureIoTClient | 6:73793bae15ba | 1018 | } |
AzureIoTClient | 6:73793bae15ba | 1019 | case MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT: |
AzureIoTClient | 6:73793bae15ba | 1020 | { |
AzureIoTClient | 6:73793bae15ba | 1021 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1022 | break; |
AzureIoTClient | 6:73793bae15ba | 1023 | } |
AzureIoTClient | 6:73793bae15ba | 1024 | case MAKE_PAYLOAD_ERROR: |
AzureIoTClient | 6:73793bae15ba | 1025 | { |
AzureIoTClient | 6:73793bae15ba | 1026 | LogError("unrecoverable errors while building a batch message\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1027 | break; |
AzureIoTClient | 6:73793bae15ba | 1028 | } |
AzureIoTClient | 6:73793bae15ba | 1029 | case MAKE_PAYLOAD_NO_ITEMS: |
AzureIoTClient | 6:73793bae15ba | 1030 | { |
AzureIoTClient | 6:73793bae15ba | 1031 | /*do nothing*/ |
AzureIoTClient | 6:73793bae15ba | 1032 | break; |
AzureIoTClient | 6:73793bae15ba | 1033 | } |
AzureIoTClient | 6:73793bae15ba | 1034 | default: |
AzureIoTClient | 6:73793bae15ba | 1035 | { |
AzureIoTClient | 6:73793bae15ba | 1036 | LogError("internal error: switch's default branch reached when never intended\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1037 | break; |
AzureIoTClient | 6:73793bae15ba | 1038 | } |
AzureIoTClient | 6:73793bae15ba | 1039 | } |
AzureIoTClient | 6:73793bae15ba | 1040 | } |
AzureIoTClient | 6:73793bae15ba | 1041 | } |
AzureIoTClient | 6:73793bae15ba | 1042 | else |
AzureIoTClient | 6:73793bae15ba | 1043 | { |
AzureIoTClient | 6:73793bae15ba | 1044 | const unsigned char* messageContent=NULL; |
AzureIoTClient | 6:73793bae15ba | 1045 | size_t messageSize=0; |
AzureIoTClient | 6:73793bae15ba | 1046 | size_t originalMessageSize=0; |
AzureIoTClient | 6:73793bae15ba | 1047 | IOTHUB_MESSAGE_LIST* message = containingRecord(handleData->waitingToSend->Flink, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 6:73793bae15ba | 1048 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 1049 | |
AzureIoTClient | 6:73793bae15ba | 1050 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_122: [The message size is computed from the length of the payload + 384.]*/ |
AzureIoTClient | 6:73793bae15ba | 1051 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1052 | (((contentType == IOTHUBMESSAGE_BYTEARRAY) && |
AzureIoTClient | 6:73793bae15ba | 1053 | (IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &originalMessageSize)==IOTHUB_MESSAGE_OK)) ? (messageSize= originalMessageSize + MAXIMUM_PAYLOAD_OVERHEAD, 1): 0) |
AzureIoTClient | 6:73793bae15ba | 1054 | |
AzureIoTClient | 6:73793bae15ba | 1055 | || |
AzureIoTClient | 6:73793bae15ba | 1056 | |
AzureIoTClient | 6:73793bae15ba | 1057 | ((contentType == IOTHUBMESSAGE_STRING) && ( |
AzureIoTClient | 6:73793bae15ba | 1058 | messageContent = (const unsigned char*)IoTHubMessage_GetString(message->messageHandle), |
AzureIoTClient | 6:73793bae15ba | 1059 | (messageSize = MAXIMUM_PAYLOAD_OVERHEAD + (originalMessageSize = ((messageContent == NULL)?0:strlen((const char*)messageContent)))), |
AzureIoTClient | 6:73793bae15ba | 1060 | messageContent!=NULL) |
AzureIoTClient | 6:73793bae15ba | 1061 | ) |
AzureIoTClient | 6:73793bae15ba | 1062 | )) |
AzureIoTClient | 6:73793bae15ba | 1063 | { |
AzureIoTClient | 6:73793bae15ba | 1064 | LogError("unable to get the message content\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1065 | /*go on...*/ |
AzureIoTClient | 6:73793bae15ba | 1066 | } |
AzureIoTClient | 6:73793bae15ba | 1067 | else |
AzureIoTClient | 6:73793bae15ba | 1068 | { |
AzureIoTClient | 6:73793bae15ba | 1069 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_109: [If the oldest message in waitingToSend causes the message to exceed the message size limit then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.]*/ |
AzureIoTClient | 6:73793bae15ba | 1070 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_121: [The message size shall be limited to 255KB -1 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1071 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 1072 | { |
AzureIoTClient | 6:73793bae15ba | 1073 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1074 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 1075 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1076 | } |
AzureIoTClient | 6:73793bae15ba | 1077 | else |
AzureIoTClient | 6:73793bae15ba | 1078 | { |
AzureIoTClient | 6:73793bae15ba | 1079 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_104: [If option SetBatching is false then _Dowork shall send individual event message as specced below.] */ |
AzureIoTClient | 6:73793bae15ba | 1080 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_105: [A clone of the event HTTP request headers shall be created.]*/ |
AzureIoTClient | 6:73793bae15ba | 1081 | HTTP_HEADERS_HANDLE clonedEventHTTPrequestHeaders = HTTPHeaders_Clone(handleData->eventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 1082 | if (clonedEventHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1083 | { |
AzureIoTClient | 6:73793bae15ba | 1084 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1085 | LogError("HTTPHeaders_Clone failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1086 | } |
AzureIoTClient | 6:73793bae15ba | 1087 | else |
AzureIoTClient | 6:73793bae15ba | 1088 | { |
AzureIoTClient | 6:73793bae15ba | 1089 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_106: [The cloned HTTP headers shall have the HTTP header "Content-Type" set to "application/octet-stream".] */ |
AzureIoTClient | 6:73793bae15ba | 1090 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_OCTET_STREAM) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1091 | { |
AzureIoTClient | 6:73793bae15ba | 1092 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1093 | LogError("HTTPHeaders_ReplaceHeaderNameValuePair failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1094 | } |
AzureIoTClient | 6:73793bae15ba | 1095 | else |
AzureIoTClient | 6:73793bae15ba | 1096 | { |
AzureIoTClient | 6:73793bae15ba | 1097 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_107: [Every message property "property":"value" shall be added to the HTTP headers as an individual header "iothub-app-property":"value".] */ |
AzureIoTClient | 6:73793bae15ba | 1098 | MAP_HANDLE map = IoTHubMessage_Properties(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 1099 | const char*const* keys; |
AzureIoTClient | 6:73793bae15ba | 1100 | const char*const* values; |
AzureIoTClient | 6:73793bae15ba | 1101 | size_t count; |
AzureIoTClient | 6:73793bae15ba | 1102 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
AzureIoTClient | 6:73793bae15ba | 1103 | { |
AzureIoTClient | 6:73793bae15ba | 1104 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1105 | LogError("unable to Map_GetInternals\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1106 | } |
AzureIoTClient | 6:73793bae15ba | 1107 | else |
AzureIoTClient | 6:73793bae15ba | 1108 | { |
AzureIoTClient | 6:73793bae15ba | 1109 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 1110 | bool goOn = true; |
AzureIoTClient | 8:1b71bf027eb5 | 1111 | const char* msgId; |
AzureIoTClient | 8:1b71bf027eb5 | 1112 | const char* corrId; |
AzureIoTClient | 8:1b71bf027eb5 | 1113 | |
AzureIoTClient | 6:73793bae15ba | 1114 | for (i = 0; (i < count) && goOn; i++) |
AzureIoTClient | 6:73793bae15ba | 1115 | { |
AzureIoTClient | 6:73793bae15ba | 1116 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_123: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1117 | messageSize += (strlen(values[i]) + strlen(keys[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
AzureIoTClient | 6:73793bae15ba | 1118 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 1119 | { |
AzureIoTClient | 6:73793bae15ba | 1120 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_121: [The message size shall be limited to 255KB -1 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1121 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1122 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 1123 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1124 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1125 | } |
AzureIoTClient | 6:73793bae15ba | 1126 | else |
AzureIoTClient | 6:73793bae15ba | 1127 | { |
AzureIoTClient | 6:73793bae15ba | 1128 | STRING_HANDLE temp = STRING_construct(IOTHUB_APP_PREFIX); |
AzureIoTClient | 6:73793bae15ba | 1129 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 1130 | { |
AzureIoTClient | 6:73793bae15ba | 1131 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1132 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1133 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1134 | } |
AzureIoTClient | 6:73793bae15ba | 1135 | else |
AzureIoTClient | 6:73793bae15ba | 1136 | { |
AzureIoTClient | 6:73793bae15ba | 1137 | if (STRING_concat(temp, keys[i]) != 0) |
AzureIoTClient | 6:73793bae15ba | 1138 | { |
AzureIoTClient | 6:73793bae15ba | 1139 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1140 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1141 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1142 | } |
AzureIoTClient | 6:73793bae15ba | 1143 | else |
AzureIoTClient | 6:73793bae15ba | 1144 | { |
AzureIoTClient | 6:73793bae15ba | 1145 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, STRING_c_str(temp), values[i]) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1146 | { |
AzureIoTClient | 6:73793bae15ba | 1147 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1148 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1149 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1150 | } |
AzureIoTClient | 6:73793bae15ba | 1151 | } |
AzureIoTClient | 6:73793bae15ba | 1152 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 1153 | } |
AzureIoTClient | 6:73793bae15ba | 1154 | } |
AzureIoTClient | 6:73793bae15ba | 1155 | } |
AzureIoTClient | 6:73793bae15ba | 1156 | |
AzureIoTClient | 7:48f0f78cd3ef | 1157 | // Add the Message Id and the Correlation Id |
AzureIoTClient | 8:1b71bf027eb5 | 1158 | msgId = IoTHubMessage_GetMessageId(message->messageHandle); |
AzureIoTClient | 7:48f0f78cd3ef | 1159 | if (goOn && msgId != NULL) |
AzureIoTClient | 7:48f0f78cd3ef | 1160 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1161 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_MESSAGE_ID, msgId) != HTTP_HEADERS_OK) |
AzureIoTClient | 7:48f0f78cd3ef | 1162 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1163 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 7:48f0f78cd3ef | 1164 | goOn = false; |
AzureIoTClient | 7:48f0f78cd3ef | 1165 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1166 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1167 | |
AzureIoTClient | 8:1b71bf027eb5 | 1168 | corrId = IoTHubMessage_GetCorrelationId(message->messageHandle); |
AzureIoTClient | 7:48f0f78cd3ef | 1169 | if (goOn && corrId != NULL) |
AzureIoTClient | 7:48f0f78cd3ef | 1170 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1171 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_CORRELATION_ID, corrId) != HTTP_HEADERS_OK) |
AzureIoTClient | 7:48f0f78cd3ef | 1172 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1173 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 7:48f0f78cd3ef | 1174 | goOn = false; |
AzureIoTClient | 7:48f0f78cd3ef | 1175 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1176 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1177 | |
AzureIoTClient | 6:73793bae15ba | 1178 | if (!goOn) |
AzureIoTClient | 6:73793bae15ba | 1179 | { |
AzureIoTClient | 6:73793bae15ba | 1180 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1181 | } |
AzureIoTClient | 6:73793bae15ba | 1182 | else |
AzureIoTClient | 6:73793bae15ba | 1183 | { |
AzureIoTClient | 6:73793bae15ba | 1184 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_110: [IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters] */ |
AzureIoTClient | 6:73793bae15ba | 1185 | BUFFER_HANDLE toBeSend = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 1186 | if (toBeSend == NULL) |
AzureIoTClient | 6:73793bae15ba | 1187 | { |
AzureIoTClient | 6:73793bae15ba | 1188 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1189 | } |
AzureIoTClient | 6:73793bae15ba | 1190 | else |
AzureIoTClient | 6:73793bae15ba | 1191 | { |
AzureIoTClient | 6:73793bae15ba | 1192 | if (BUFFER_build(toBeSend, messageContent, originalMessageSize) != 0) |
AzureIoTClient | 6:73793bae15ba | 1193 | { |
AzureIoTClient | 6:73793bae15ba | 1194 | LogError("unable to BUFFER_build\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1195 | } |
AzureIoTClient | 6:73793bae15ba | 1196 | else |
AzureIoTClient | 6:73793bae15ba | 1197 | { |
AzureIoTClient | 6:73793bae15ba | 1198 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1199 | HTTPAPIEX_RESULT r; |
AzureIoTClient | 6:73793bae15ba | 1200 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1201 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1202 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1203 | HTTPAPI_REQUEST_POST, |
AzureIoTClient | 6:73793bae15ba | 1204 | STRING_c_str(handleData->eventHTTPrelativePath), |
AzureIoTClient | 6:73793bae15ba | 1205 | clonedEventHTTPrequestHeaders, |
AzureIoTClient | 6:73793bae15ba | 1206 | toBeSend, |
AzureIoTClient | 6:73793bae15ba | 1207 | &statusCode, |
AzureIoTClient | 6:73793bae15ba | 1208 | NULL, |
AzureIoTClient | 6:73793bae15ba | 1209 | NULL |
AzureIoTClient | 6:73793bae15ba | 1210 | )) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1211 | { |
AzureIoTClient | 6:73793bae15ba | 1212 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1213 | } |
AzureIoTClient | 6:73793bae15ba | 1214 | else |
AzureIoTClient | 6:73793bae15ba | 1215 | { |
AzureIoTClient | 6:73793bae15ba | 1216 | if (statusCode < 300) |
AzureIoTClient | 6:73793bae15ba | 1217 | { |
AzureIoTClient | 6:73793bae15ba | 1218 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_112: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list the item send, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCCESS. The item shall be removed from waitingToSend.] */ |
AzureIoTClient | 6:73793bae15ba | 1219 | PDLIST_ENTRY justSent = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1220 | DList_InsertTailList(&(handleData->eventConfirmations), justSent); |
AzureIoTClient | 6:73793bae15ba | 1221 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1222 | } |
AzureIoTClient | 6:73793bae15ba | 1223 | else |
AzureIoTClient | 6:73793bae15ba | 1224 | { |
AzureIoTClient | 6:73793bae15ba | 1225 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_111: [If HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 1226 | LogError("unexpected HTTP status code (%u)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1227 | } |
AzureIoTClient | 6:73793bae15ba | 1228 | } |
AzureIoTClient | 6:73793bae15ba | 1229 | } |
AzureIoTClient | 6:73793bae15ba | 1230 | BUFFER_delete(toBeSend); |
AzureIoTClient | 6:73793bae15ba | 1231 | } |
AzureIoTClient | 6:73793bae15ba | 1232 | } |
AzureIoTClient | 6:73793bae15ba | 1233 | } |
AzureIoTClient | 6:73793bae15ba | 1234 | } |
AzureIoTClient | 6:73793bae15ba | 1235 | HTTPHeaders_Free(clonedEventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 1236 | } |
AzureIoTClient | 6:73793bae15ba | 1237 | } |
AzureIoTClient | 6:73793bae15ba | 1238 | } |
AzureIoTClient | 6:73793bae15ba | 1239 | } |
AzureIoTClient | 6:73793bae15ba | 1240 | } |
AzureIoTClient | 6:73793bae15ba | 1241 | } |
AzureIoTClient | 6:73793bae15ba | 1242 | |
AzureIoTClient | 6:73793bae15ba | 1243 | #define ACTION_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 1244 | ABANDON, \ |
AzureIoTClient | 6:73793bae15ba | 1245 | REJECT, \ |
AzureIoTClient | 6:73793bae15ba | 1246 | ACCEPT |
AzureIoTClient | 6:73793bae15ba | 1247 | DEFINE_ENUM(ACTION, ACTION_VALUES); |
AzureIoTClient | 6:73793bae15ba | 1248 | |
AzureIoTClient | 6:73793bae15ba | 1249 | static void abandonOrAcceptMessage(HTTPTRANSPORT_HANDLE_DATA* handleData, const char* ETag, ACTION action) |
AzureIoTClient | 6:73793bae15ba | 1250 | { |
AzureIoTClient | 6:73793bae15ba | 1251 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_050: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1252 | -requestType: POST |
AzureIoTClient | 6:73793bae15ba | 1253 | -relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview" |
AzureIoTClient | 6:73793bae15ba | 1254 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1255 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1256 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1257 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1258 | - statusCode: a pointer to unsigned int which might be examined for logging |
AzureIoTClient | 6:73793bae15ba | 1259 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1260 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1261 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_051: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1262 | -requestType: DELETE |
AzureIoTClient | 6:73793bae15ba | 1263 | -relativePath: abandon relative path begin + value of ETag + "?api-version=2015-08-15-preview" |
AzureIoTClient | 6:73793bae15ba | 1264 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1265 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1266 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1267 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1268 | - statusCode: a pointer to unsigned int which might be used by logging |
AzureIoTClient | 6:73793bae15ba | 1269 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1270 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1271 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_077: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1272 | -requestType: DELETE |
AzureIoTClient | 6:73793bae15ba | 1273 | -relativePath: abandon relative path begin + value of ETag +"?api-version=2015-08-15-preview" + "&reject" |
AzureIoTClient | 6:73793bae15ba | 1274 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1275 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1276 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1277 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1278 | - statusCode: a pointer to unsigned int which might be used by logging |
AzureIoTClient | 6:73793bae15ba | 1279 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1280 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1281 | |
AzureIoTClient | 6:73793bae15ba | 1282 | STRING_HANDLE fullAbandonRelativePath = STRING_clone(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 1283 | if (fullAbandonRelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 1284 | { |
AzureIoTClient | 6:73793bae15ba | 1285 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1286 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1287 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1288 | LogError("unable to STRING_clone\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1289 | } |
AzureIoTClient | 6:73793bae15ba | 1290 | else |
AzureIoTClient | 6:73793bae15ba | 1291 | { |
AzureIoTClient | 6:73793bae15ba | 1292 | STRING_HANDLE ETagUnquoted = STRING_construct_n(ETag + 1, strlen(ETag) - 2); /*skip first character which is '"' and the last one (which is also '"')*/ |
AzureIoTClient | 6:73793bae15ba | 1293 | if (ETagUnquoted == NULL) |
AzureIoTClient | 6:73793bae15ba | 1294 | { |
AzureIoTClient | 6:73793bae15ba | 1295 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1296 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1297 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1298 | LogError("unable to STRING_construct_n\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1299 | } |
AzureIoTClient | 6:73793bae15ba | 1300 | else |
AzureIoTClient | 6:73793bae15ba | 1301 | { |
AzureIoTClient | 6:73793bae15ba | 1302 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1303 | (STRING_concat_with_STRING(fullAbandonRelativePath, ETagUnquoted) == 0) && |
AzureIoTClient | 6:73793bae15ba | 1304 | (STRING_concat(fullAbandonRelativePath, (action == ABANDON) ? "/abandon" API_VERSION : ((action == REJECT) ? API_VERSION "&reject" : API_VERSION)) == 0) |
AzureIoTClient | 6:73793bae15ba | 1305 | )) |
AzureIoTClient | 6:73793bae15ba | 1306 | { |
AzureIoTClient | 6:73793bae15ba | 1307 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1308 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1309 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1310 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1311 | } |
AzureIoTClient | 6:73793bae15ba | 1312 | else |
AzureIoTClient | 6:73793bae15ba | 1313 | { |
AzureIoTClient | 6:73793bae15ba | 1314 | HTTP_HEADERS_HANDLE abandonRequestHttpHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 1315 | if (abandonRequestHttpHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1316 | { |
AzureIoTClient | 6:73793bae15ba | 1317 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1318 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1319 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1320 | LogError("unable to HTTPHeaders_Alloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1321 | } |
AzureIoTClient | 6:73793bae15ba | 1322 | else |
AzureIoTClient | 6:73793bae15ba | 1323 | { |
AzureIoTClient | 6:73793bae15ba | 1324 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1325 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", " ") == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 1326 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "If-Match", ETag) == HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1327 | )) |
AzureIoTClient | 6:73793bae15ba | 1328 | { |
AzureIoTClient | 6:73793bae15ba | 1329 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1330 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1331 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1332 | LogError("unable to HTTPHeaders_AddHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1333 | } |
AzureIoTClient | 6:73793bae15ba | 1334 | else |
AzureIoTClient | 6:73793bae15ba | 1335 | { |
AzureIoTClient | 6:73793bae15ba | 1336 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1337 | if (HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1338 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1339 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1340 | (action == ABANDON) ? HTTPAPI_REQUEST_POST : HTTPAPI_REQUEST_DELETE, /*-requestType: POST */ |
AzureIoTClient | 6:73793bae15ba | 1341 | STRING_c_str(fullAbandonRelativePath), /*-relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview" */ |
AzureIoTClient | 6:73793bae15ba | 1342 | abandonRequestHttpHeaders, /*- requestHttpHeadersHandle: an HTTP headers instance containing the following */ |
AzureIoTClient | 6:73793bae15ba | 1343 | NULL, /*- requestContent: NULL */ |
AzureIoTClient | 6:73793bae15ba | 1344 | &statusCode, /*- statusCode: a pointer to unsigned int which might be examined for logging */ |
AzureIoTClient | 6:73793bae15ba | 1345 | NULL, /*- responseHeadearsHandle: NULL */ |
AzureIoTClient | 6:73793bae15ba | 1346 | NULL /*- responseContent: NULL] */ |
AzureIoTClient | 6:73793bae15ba | 1347 | ) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1348 | { |
AzureIoTClient | 6:73793bae15ba | 1349 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1350 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1351 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1352 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1353 | } |
AzureIoTClient | 6:73793bae15ba | 1354 | else |
AzureIoTClient | 6:73793bae15ba | 1355 | { |
AzureIoTClient | 6:73793bae15ba | 1356 | if (statusCode != 204) |
AzureIoTClient | 6:73793bae15ba | 1357 | { |
AzureIoTClient | 6:73793bae15ba | 1358 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1359 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1360 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1361 | LogError("unexpected status code returned %u (was expecting 204)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1362 | } |
AzureIoTClient | 6:73793bae15ba | 1363 | else |
AzureIoTClient | 6:73793bae15ba | 1364 | { |
AzureIoTClient | 6:73793bae15ba | 1365 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1366 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1367 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1368 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 1369 | } |
AzureIoTClient | 6:73793bae15ba | 1370 | } |
AzureIoTClient | 6:73793bae15ba | 1371 | } |
AzureIoTClient | 6:73793bae15ba | 1372 | HTTPHeaders_Free(abandonRequestHttpHeaders); |
AzureIoTClient | 6:73793bae15ba | 1373 | } |
AzureIoTClient | 6:73793bae15ba | 1374 | } |
AzureIoTClient | 6:73793bae15ba | 1375 | STRING_delete(ETagUnquoted); |
AzureIoTClient | 6:73793bae15ba | 1376 | } |
AzureIoTClient | 6:73793bae15ba | 1377 | STRING_delete(fullAbandonRelativePath); |
AzureIoTClient | 6:73793bae15ba | 1378 | } |
AzureIoTClient | 6:73793bae15ba | 1379 | } |
AzureIoTClient | 6:73793bae15ba | 1380 | |
AzureIoTClient | 6:73793bae15ba | 1381 | static void DoMessages(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1382 | { |
AzureIoTClient | 6:73793bae15ba | 1383 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1384 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_057: [If flag DoWork_PullMessage is set to false then _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1385 | if (handleData->DoWork_PullMessage) |
AzureIoTClient | 6:73793bae15ba | 1386 | { |
AzureIoTClient | 6:73793bae15ba | 1387 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_116: [After client creation, the first GET shall be allowed no matter what the value of GetMinimumPollingTime.] */ |
AzureIoTClient | 6:73793bae15ba | 1388 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_117: [If time is not available then all calls shall be treated as if they are the first one.] */ |
AzureIoTClient | 6:73793bae15ba | 1389 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_115: [A GET request that happens earlier than GetMinimumPollingTime shall be ignored.] */ |
AzureIoTClient | 6:73793bae15ba | 1390 | time_t timeNow = get_time(NULL); |
AzureIoTClient | 6:73793bae15ba | 1391 | bool isPollingAllowed = handleData->isFirstPoll || (timeNow == (time_t)(-1)) || (get_difftime(timeNow, handleData->lastPollTime) > handleData->getMinimumPollingTime); |
AzureIoTClient | 6:73793bae15ba | 1392 | if (isPollingAllowed) |
AzureIoTClient | 6:73793bae15ba | 1393 | { |
AzureIoTClient | 6:73793bae15ba | 1394 | HTTP_HEADERS_HANDLE responseHTTPHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 1395 | if (responseHTTPHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1396 | { |
AzureIoTClient | 6:73793bae15ba | 1397 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1398 | LogError("unable to HTTPHeaders_Alloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1399 | } |
AzureIoTClient | 6:73793bae15ba | 1400 | else |
AzureIoTClient | 6:73793bae15ba | 1401 | { |
AzureIoTClient | 6:73793bae15ba | 1402 | BUFFER_HANDLE responseContent = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 1403 | if (responseContent == NULL) |
AzureIoTClient | 6:73793bae15ba | 1404 | { |
AzureIoTClient | 6:73793bae15ba | 1405 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1406 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1407 | } |
AzureIoTClient | 6:73793bae15ba | 1408 | else |
AzureIoTClient | 6:73793bae15ba | 1409 | { |
AzureIoTClient | 6:73793bae15ba | 1410 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1411 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_036: [Otherwise, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters |
AzureIoTClient | 6:73793bae15ba | 1412 | requestType: GET |
AzureIoTClient | 6:73793bae15ba | 1413 | relativePath: the message HTTP relative path |
AzureIoTClient | 6:73793bae15ba | 1414 | requestHttpHeadersHandle: message HTTP request headers created by _Create |
AzureIoTClient | 6:73793bae15ba | 1415 | requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1416 | statusCode: a pointer to unsigned int which shall be later examined |
AzureIoTClient | 6:73793bae15ba | 1417 | responseHeadearsHandle: a new instance of HTTP headers |
AzureIoTClient | 6:73793bae15ba | 1418 | responseContent: a new instance of buffer] |
AzureIoTClient | 6:73793bae15ba | 1419 | */ |
AzureIoTClient | 6:73793bae15ba | 1420 | if (HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1421 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1422 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1423 | HTTPAPI_REQUEST_GET, /*requestType: GET*/ |
AzureIoTClient | 6:73793bae15ba | 1424 | STRING_c_str(handleData->messageHTTPrelativePath), /*relativePath: the message HTTP relative path*/ |
AzureIoTClient | 6:73793bae15ba | 1425 | handleData->messageHTTPrequestHeaders, /*requestHttpHeadersHandle: message HTTP request headers created by _Create*/ |
AzureIoTClient | 6:73793bae15ba | 1426 | NULL, /*requestContent: NULL*/ |
AzureIoTClient | 6:73793bae15ba | 1427 | &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/ |
AzureIoTClient | 6:73793bae15ba | 1428 | responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/ |
AzureIoTClient | 6:73793bae15ba | 1429 | responseContent /*responseContent: a new instance of buffer*/ |
AzureIoTClient | 6:73793bae15ba | 1430 | ) |
AzureIoTClient | 6:73793bae15ba | 1431 | != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1432 | { |
AzureIoTClient | 6:73793bae15ba | 1433 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1434 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1435 | } |
AzureIoTClient | 6:73793bae15ba | 1436 | else |
AzureIoTClient | 6:73793bae15ba | 1437 | { |
AzureIoTClient | 6:73793bae15ba | 1438 | /*HTTP dialogue was succesfull*/ |
AzureIoTClient | 6:73793bae15ba | 1439 | if (timeNow == (time_t)(-1)) |
AzureIoTClient | 6:73793bae15ba | 1440 | { |
AzureIoTClient | 6:73793bae15ba | 1441 | handleData->isFirstPoll = true; |
AzureIoTClient | 6:73793bae15ba | 1442 | } |
AzureIoTClient | 6:73793bae15ba | 1443 | else |
AzureIoTClient | 6:73793bae15ba | 1444 | { |
AzureIoTClient | 6:73793bae15ba | 1445 | handleData->isFirstPoll = false; |
AzureIoTClient | 6:73793bae15ba | 1446 | handleData->lastPollTime = timeNow; |
AzureIoTClient | 6:73793bae15ba | 1447 | } |
AzureIoTClient | 6:73793bae15ba | 1448 | if (statusCode == 204) |
AzureIoTClient | 6:73793bae15ba | 1449 | { |
AzureIoTClient | 6:73793bae15ba | 1450 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1451 | /*this is an expected status code, means "no commands", but logging that creates panic*/ |
AzureIoTClient | 6:73793bae15ba | 1452 | |
AzureIoTClient | 6:73793bae15ba | 1453 | /*do nothing, advance to next action*/ |
AzureIoTClient | 6:73793bae15ba | 1454 | } |
AzureIoTClient | 6:73793bae15ba | 1455 | else if (statusCode != 200) |
AzureIoTClient | 6:73793bae15ba | 1456 | { |
AzureIoTClient | 6:73793bae15ba | 1457 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1458 | LogError("expected status code was 200, but actually was received %u... moving on\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1459 | } |
AzureIoTClient | 6:73793bae15ba | 1460 | else |
AzureIoTClient | 6:73793bae15ba | 1461 | { |
AzureIoTClient | 6:73793bae15ba | 1462 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_039: [If status code is 200, then _DoWork shall make a copy of the value of the "ETag" http header.]*/ |
AzureIoTClient | 6:73793bae15ba | 1463 | const char* etagValue = HTTPHeaders_FindHeaderValue(responseHTTPHeaders, "ETag"); |
AzureIoTClient | 6:73793bae15ba | 1464 | if (etagValue == NULL) |
AzureIoTClient | 6:73793bae15ba | 1465 | { |
AzureIoTClient | 6:73793bae15ba | 1466 | LogError("unable to find a received header called \"E-Tag\"\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1467 | } |
AzureIoTClient | 6:73793bae15ba | 1468 | else |
AzureIoTClient | 6:73793bae15ba | 1469 | { |
AzureIoTClient | 6:73793bae15ba | 1470 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_040: [If no such header is found or is invalid, then _DoWork shall advance to the next action.]*/ |
AzureIoTClient | 6:73793bae15ba | 1471 | size_t etagsize = strlen(etagValue); |
AzureIoTClient | 6:73793bae15ba | 1472 | if ( |
AzureIoTClient | 6:73793bae15ba | 1473 | (etagsize < 2) || |
AzureIoTClient | 6:73793bae15ba | 1474 | (etagValue[0] != '"') || |
AzureIoTClient | 6:73793bae15ba | 1475 | (etagValue[etagsize - 1] != '"') |
AzureIoTClient | 6:73793bae15ba | 1476 | ) |
AzureIoTClient | 6:73793bae15ba | 1477 | { |
AzureIoTClient | 6:73793bae15ba | 1478 | LogError("ETag is not a valid quoted string\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1479 | } |
AzureIoTClient | 6:73793bae15ba | 1480 | else |
AzureIoTClient | 6:73793bae15ba | 1481 | { |
AzureIoTClient | 6:73793bae15ba | 1482 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_041: [_DoWork shall assemble an IOTHUBMESSAGE_HANDLE from the received HTTP content (using the responseContent buffer).] */ |
AzureIoTClient | 6:73793bae15ba | 1483 | IOTHUB_MESSAGE_HANDLE receivedMessage = IoTHubMessage_CreateFromByteArray(BUFFER_u_char(responseContent), BUFFER_length(responseContent)); |
AzureIoTClient | 6:73793bae15ba | 1484 | if (receivedMessage == NULL) |
AzureIoTClient | 6:73793bae15ba | 1485 | { |
AzureIoTClient | 6:73793bae15ba | 1486 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_042: [If assembling the message fails in any way, then _DoWork shall "abandon" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1487 | LogError("unable to IoTHubMessage_CreateFromByteArray, trying to abandon the message... \r\n"); |
AzureIoTClient | 6:73793bae15ba | 1488 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1489 | } |
AzureIoTClient | 6:73793bae15ba | 1490 | else |
AzureIoTClient | 6:73793bae15ba | 1491 | { |
AzureIoTClient | 6:73793bae15ba | 1492 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_087: [All the HTTP headers of the form iothub-app-name:somecontent shall be transformed in message properties {name, somecontent}.]*/ |
AzureIoTClient | 7:48f0f78cd3ef | 1493 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_07_008: [The HTTP header of iothub-messageid shall be set in the MessageId.]*/ |
AzureIoTClient | 6:73793bae15ba | 1494 | size_t nHeaders; |
AzureIoTClient | 6:73793bae15ba | 1495 | if (HTTPHeaders_GetHeaderCount(responseHTTPHeaders, &nHeaders) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1496 | { |
AzureIoTClient | 6:73793bae15ba | 1497 | LogError("unable to get the count of HTTP headers\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1498 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1499 | } |
AzureIoTClient | 6:73793bae15ba | 1500 | else |
AzureIoTClient | 6:73793bae15ba | 1501 | { |
AzureIoTClient | 6:73793bae15ba | 1502 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 1503 | MAP_HANDLE properties = (nHeaders > 0) ? IoTHubMessage_Properties(receivedMessage) : NULL; |
AzureIoTClient | 6:73793bae15ba | 1504 | for (i = 0; i < nHeaders; i++) |
AzureIoTClient | 6:73793bae15ba | 1505 | { |
AzureIoTClient | 6:73793bae15ba | 1506 | char* completeHeader; |
AzureIoTClient | 6:73793bae15ba | 1507 | if (HTTPHeaders_GetHeader(responseHTTPHeaders, i, &completeHeader) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1508 | { |
AzureIoTClient | 6:73793bae15ba | 1509 | break; |
AzureIoTClient | 6:73793bae15ba | 1510 | } |
AzureIoTClient | 6:73793bae15ba | 1511 | else |
AzureIoTClient | 6:73793bae15ba | 1512 | { |
AzureIoTClient | 6:73793bae15ba | 1513 | if (strncmp(IOTHUB_APP_PREFIX, completeHeader, strlen(IOTHUB_APP_PREFIX)) == 0) |
AzureIoTClient | 6:73793bae15ba | 1514 | { |
AzureIoTClient | 6:73793bae15ba | 1515 | /*looks like a property headers*/ |
AzureIoTClient | 6:73793bae15ba | 1516 | /*there's a guaranteed ':' in the completeHeader, by HTTP_HEADERS module*/ |
AzureIoTClient | 6:73793bae15ba | 1517 | char* whereIsColon = strchr(completeHeader, ':'); |
AzureIoTClient | 7:48f0f78cd3ef | 1518 | if (whereIsColon != NULL) |
AzureIoTClient | 7:48f0f78cd3ef | 1519 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1520 | *whereIsColon = '\0'; /*cut it down*/ |
AzureIoTClient | 7:48f0f78cd3ef | 1521 | if (Map_AddOrUpdate(properties, completeHeader + strlen(IOTHUB_APP_PREFIX), whereIsColon + 2) != MAP_OK) /*whereIsColon+1 is a space because HTTPEHADERS outputs a ": " between name and value*/ |
AzureIoTClient | 7:48f0f78cd3ef | 1522 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1523 | free(completeHeader); |
AzureIoTClient | 7:48f0f78cd3ef | 1524 | break; |
AzureIoTClient | 7:48f0f78cd3ef | 1525 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1526 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1527 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1528 | else if (strncmp(IOTHUB_MESSAGE_ID, completeHeader, strlen(IOTHUB_MESSAGE_ID)) == 0) |
AzureIoTClient | 7:48f0f78cd3ef | 1529 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1530 | char* whereIsColon = strchr(completeHeader, ':'); |
AzureIoTClient | 7:48f0f78cd3ef | 1531 | if (whereIsColon != NULL) |
AzureIoTClient | 6:73793bae15ba | 1532 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1533 | *whereIsColon = '\0'; /*cut it down*/ |
AzureIoTClient | 7:48f0f78cd3ef | 1534 | if (IoTHubMessage_SetMessageId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 7:48f0f78cd3ef | 1535 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1536 | free(completeHeader); |
AzureIoTClient | 7:48f0f78cd3ef | 1537 | break; |
AzureIoTClient | 7:48f0f78cd3ef | 1538 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1539 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1540 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1541 | else if (strncmp(IOTHUB_CORRELATION_ID, completeHeader, strlen(IOTHUB_CORRELATION_ID)) == 0) |
AzureIoTClient | 7:48f0f78cd3ef | 1542 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1543 | char* whereIsColon = strchr(completeHeader, ':'); |
AzureIoTClient | 7:48f0f78cd3ef | 1544 | if (whereIsColon != NULL) |
AzureIoTClient | 7:48f0f78cd3ef | 1545 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1546 | *whereIsColon = '\0'; /*cut it down*/ |
AzureIoTClient | 7:48f0f78cd3ef | 1547 | if (IoTHubMessage_SetCorrelationId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 7:48f0f78cd3ef | 1548 | { |
AzureIoTClient | 7:48f0f78cd3ef | 1549 | free(completeHeader); |
AzureIoTClient | 7:48f0f78cd3ef | 1550 | break; |
AzureIoTClient | 7:48f0f78cd3ef | 1551 | } |
AzureIoTClient | 6:73793bae15ba | 1552 | } |
AzureIoTClient | 6:73793bae15ba | 1553 | } |
AzureIoTClient | 6:73793bae15ba | 1554 | free(completeHeader); |
AzureIoTClient | 6:73793bae15ba | 1555 | } |
AzureIoTClient | 6:73793bae15ba | 1556 | } |
AzureIoTClient | 6:73793bae15ba | 1557 | |
AzureIoTClient | 6:73793bae15ba | 1558 | if (i < nHeaders) |
AzureIoTClient | 6:73793bae15ba | 1559 | { |
AzureIoTClient | 6:73793bae15ba | 1560 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1561 | } |
AzureIoTClient | 6:73793bae15ba | 1562 | else |
AzureIoTClient | 6:73793bae15ba | 1563 | { |
AzureIoTClient | 6:73793bae15ba | 1564 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_043: [Otherwise, _DoWork shall call IoTHubClient_LL_MessageCallback with parameters handle = iotHubClientHandle and message = newly created message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1565 | IOTHUBMESSAGE_DISPOSITION_RESULT messageResult = IoTHubClient_LL_MessageCallback(iotHubClientHandle, receivedMessage); |
AzureIoTClient | 6:73793bae15ba | 1566 | if (messageResult == IOTHUBMESSAGE_ACCEPTED) |
AzureIoTClient | 6:73793bae15ba | 1567 | { |
AzureIoTClient | 6:73793bae15ba | 1568 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_044: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ACCEPTED then _DoWork shall "accept" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1569 | abandonOrAcceptMessage(handle, etagValue, ACCEPT); |
AzureIoTClient | 6:73793bae15ba | 1570 | } |
AzureIoTClient | 6:73793bae15ba | 1571 | else if (messageResult == IOTHUBMESSAGE_REJECTED) |
AzureIoTClient | 6:73793bae15ba | 1572 | { |
AzureIoTClient | 6:73793bae15ba | 1573 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_074: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_REJECTED then _DoWork shall "reject" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1574 | abandonOrAcceptMessage(handle, etagValue, REJECT); |
AzureIoTClient | 6:73793bae15ba | 1575 | } |
AzureIoTClient | 6:73793bae15ba | 1576 | else |
AzureIoTClient | 6:73793bae15ba | 1577 | { |
AzureIoTClient | 6:73793bae15ba | 1578 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_079: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ABANDONED then _DoWork shall "abandon" the message.] */ |
AzureIoTClient | 6:73793bae15ba | 1579 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1580 | } |
AzureIoTClient | 6:73793bae15ba | 1581 | } |
AzureIoTClient | 6:73793bae15ba | 1582 | } |
AzureIoTClient | 6:73793bae15ba | 1583 | IoTHubMessage_Destroy(receivedMessage); |
AzureIoTClient | 6:73793bae15ba | 1584 | } |
AzureIoTClient | 6:73793bae15ba | 1585 | } |
AzureIoTClient | 6:73793bae15ba | 1586 | |
AzureIoTClient | 6:73793bae15ba | 1587 | } |
AzureIoTClient | 6:73793bae15ba | 1588 | } |
AzureIoTClient | 6:73793bae15ba | 1589 | } |
AzureIoTClient | 6:73793bae15ba | 1590 | BUFFER_delete(responseContent); |
AzureIoTClient | 6:73793bae15ba | 1591 | } |
AzureIoTClient | 6:73793bae15ba | 1592 | HTTPHeaders_Free(responseHTTPHeaders); |
AzureIoTClient | 6:73793bae15ba | 1593 | } |
AzureIoTClient | 6:73793bae15ba | 1594 | } |
AzureIoTClient | 6:73793bae15ba | 1595 | else |
AzureIoTClient | 6:73793bae15ba | 1596 | { |
AzureIoTClient | 6:73793bae15ba | 1597 | /*isPollingAllowed is false... */ |
AzureIoTClient | 6:73793bae15ba | 1598 | /*do nothing "shall be ignored*/ |
AzureIoTClient | 6:73793bae15ba | 1599 | } |
AzureIoTClient | 6:73793bae15ba | 1600 | } |
AzureIoTClient | 6:73793bae15ba | 1601 | } |
AzureIoTClient | 6:73793bae15ba | 1602 | |
AzureIoTClient | 6:73793bae15ba | 1603 | void IoTHubTransportHttp_DoWork(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1604 | { |
AzureIoTClient | 6:73793bae15ba | 1605 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_017: [If parameter handle is NULL or parameter iotHubClientHandle then IoTHubTransportHttp_DoWork shall immeditely return.]*/ |
AzureIoTClient | 6:73793bae15ba | 1606 | if ((handle != NULL) && (iotHubClientHandle != NULL)) |
AzureIoTClient | 6:73793bae15ba | 1607 | { |
AzureIoTClient | 6:73793bae15ba | 1608 | DoEvent(handle, iotHubClientHandle); |
AzureIoTClient | 6:73793bae15ba | 1609 | DoMessages(handle, iotHubClientHandle); |
AzureIoTClient | 6:73793bae15ba | 1610 | } |
AzureIoTClient | 6:73793bae15ba | 1611 | } |
AzureIoTClient | 6:73793bae15ba | 1612 | |
AzureIoTClient | 6:73793bae15ba | 1613 | IOTHUB_CLIENT_RESULT IoTHubTransportHttp_GetSendStatus(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 6:73793bae15ba | 1614 | { |
AzureIoTClient | 6:73793bae15ba | 1615 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 1616 | |
AzureIoTClient | 6:73793bae15ba | 1617 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_001: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */ |
AzureIoTClient | 6:73793bae15ba | 1618 | if (handle == NULL) |
AzureIoTClient | 6:73793bae15ba | 1619 | { |
AzureIoTClient | 6:73793bae15ba | 1620 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1621 | LogError("Invalid handle to IoTHubClient HTTP transport instance.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1622 | } |
AzureIoTClient | 6:73793bae15ba | 1623 | else if (iotHubClientStatus == NULL) |
AzureIoTClient | 6:73793bae15ba | 1624 | { |
AzureIoTClient | 6:73793bae15ba | 1625 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1626 | LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1627 | } |
AzureIoTClient | 6:73793bae15ba | 1628 | else |
AzureIoTClient | 6:73793bae15ba | 1629 | { |
AzureIoTClient | 6:73793bae15ba | 1630 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1631 | |
AzureIoTClient | 6:73793bae15ba | 1632 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_002: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent] */ |
AzureIoTClient | 6:73793bae15ba | 1633 | if (!DList_IsListEmpty(handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 1634 | { |
AzureIoTClient | 6:73793bae15ba | 1635 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY; |
AzureIoTClient | 6:73793bae15ba | 1636 | } |
AzureIoTClient | 6:73793bae15ba | 1637 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_003: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently event items to be sent or being sent] */ |
AzureIoTClient | 6:73793bae15ba | 1638 | else |
AzureIoTClient | 6:73793bae15ba | 1639 | { |
AzureIoTClient | 6:73793bae15ba | 1640 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE; |
AzureIoTClient | 6:73793bae15ba | 1641 | } |
AzureIoTClient | 6:73793bae15ba | 1642 | |
AzureIoTClient | 6:73793bae15ba | 1643 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1644 | } |
AzureIoTClient | 6:73793bae15ba | 1645 | |
AzureIoTClient | 6:73793bae15ba | 1646 | return result; |
AzureIoTClient | 6:73793bae15ba | 1647 | } |
AzureIoTClient | 6:73793bae15ba | 1648 | |
AzureIoTClient | 6:73793bae15ba | 1649 | IOTHUB_CLIENT_RESULT IoTHubTransportHttp_SetOption(TRANSPORT_HANDLE handle, const char* option, const void* value) |
AzureIoTClient | 6:73793bae15ba | 1650 | { |
AzureIoTClient | 6:73793bae15ba | 1651 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 1652 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_080: [If handle parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1653 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_081: [If option parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1654 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_082: [If value parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1655 | if ( |
AzureIoTClient | 6:73793bae15ba | 1656 | (handle == NULL) || |
AzureIoTClient | 6:73793bae15ba | 1657 | (option == NULL) || |
AzureIoTClient | 6:73793bae15ba | 1658 | (value == NULL) |
AzureIoTClient | 6:73793bae15ba | 1659 | ) |
AzureIoTClient | 6:73793bae15ba | 1660 | { |
AzureIoTClient | 6:73793bae15ba | 1661 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1662 | LogError("invalid parameter (NULL) passed to IoTHubTransportHttp_SetOption\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1663 | } |
AzureIoTClient | 6:73793bae15ba | 1664 | else |
AzureIoTClient | 6:73793bae15ba | 1665 | { |
AzureIoTClient | 6:73793bae15ba | 1666 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1667 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_097: ["Batching"] */ |
AzureIoTClient | 6:73793bae15ba | 1668 | if (strcmp("Batching", option) == 0) |
AzureIoTClient | 6:73793bae15ba | 1669 | { |
AzureIoTClient | 6:73793bae15ba | 1670 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_083: [If optionName is an option handled by IoTHubTransportHttp then it shall be set.] */ |
AzureIoTClient | 6:73793bae15ba | 1671 | handleData->doBatchedTransfers = *(bool*)value; |
AzureIoTClient | 6:73793bae15ba | 1672 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1673 | } |
AzureIoTClient | 6:73793bae15ba | 1674 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_114: ["MinimumPollingTime"] */ |
AzureIoTClient | 6:73793bae15ba | 1675 | else if (strcmp("MinimumPollingTime", option) == 0) |
AzureIoTClient | 6:73793bae15ba | 1676 | { |
AzureIoTClient | 6:73793bae15ba | 1677 | handleData->getMinimumPollingTime = *(unsigned int*)value; |
AzureIoTClient | 6:73793bae15ba | 1678 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1679 | } |
AzureIoTClient | 6:73793bae15ba | 1680 | else |
AzureIoTClient | 6:73793bae15ba | 1681 | { |
AzureIoTClient | 6:73793bae15ba | 1682 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_084: [Otherwise, IoTHubTransport_Http shall call HTTPAPIEX_SetOption with the same parameters and return the translated code.] */ |
AzureIoTClient | 6:73793bae15ba | 1683 | HTTPAPIEX_RESULT HTTPAPIEX_result = HTTPAPIEX_SetOption(handleData->httpApiExHandle, option, value); |
AzureIoTClient | 6:73793bae15ba | 1684 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_086: [The following table translates HTTPAPIEX return codes to IOTHUB_CLIENT_RESULT return codes:] */ |
AzureIoTClient | 6:73793bae15ba | 1685 | if (HTTPAPIEX_result == HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1686 | { |
AzureIoTClient | 6:73793bae15ba | 1687 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1688 | } |
AzureIoTClient | 6:73793bae15ba | 1689 | else if (HTTPAPIEX_result == HTTPAPIEX_INVALID_ARG) |
AzureIoTClient | 6:73793bae15ba | 1690 | { |
AzureIoTClient | 6:73793bae15ba | 1691 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1692 | LogError("HTTPAPIEX_SetOption failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1693 | } |
AzureIoTClient | 6:73793bae15ba | 1694 | else |
AzureIoTClient | 6:73793bae15ba | 1695 | { |
AzureIoTClient | 6:73793bae15ba | 1696 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 6:73793bae15ba | 1697 | LogError("HTTPAPIEX_SetOption failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1698 | } |
AzureIoTClient | 6:73793bae15ba | 1699 | } |
AzureIoTClient | 6:73793bae15ba | 1700 | } |
AzureIoTClient | 6:73793bae15ba | 1701 | return result; |
AzureIoTClient | 6:73793bae15ba | 1702 | } |