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