rhodes 7.4.1 → 7.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (165) hide show
  1. checksums.yaml +5 -5
  2. data/CREDITS +3 -1
  3. data/LICENSE +1 -1
  4. data/README.md +3 -1
  5. data/Rakefile +52 -43
  6. data/SECURITY.md +19 -0
  7. data/appveyor.yml +60 -7
  8. data/azure-pipelines.yml +67 -0
  9. data/extensions/rhoconnect-push/ext/rhoconnect-push/platform/android/src/com/rhomobile/rhoelements/ans/ANSManager.java +2 -2
  10. data/lib/commonAPI/barcode/ext/platform/android/AndroidManifest.xml +3 -1
  11. data/lib/commonAPI/barcode/ext/platform/android/src/com/google/barcodereader/BarcodeCaptureActivity.java +11 -13
  12. data/lib/commonAPI/barcode/ext/platform/android/src/com/google/barcodereader/BarcodeGraphicTracker.java +1 -1
  13. data/lib/commonAPI/barcode/ext/platform/android/src/com/google/barcodereader/ui/camera/CameraSource.java +3 -3
  14. data/lib/commonAPI/barcode/ext/platform/android/src/com/google/barcodereader/ui/camera/CameraSourcePreview.java +1 -1
  15. data/lib/commonAPI/barcode/ext.yml +4 -8
  16. data/lib/commonAPI/coreapi/ext/platform/android/Rakefile +0 -12
  17. data/lib/commonAPI/coreapi/ext/platform/android/src/com/rho/notification/Notification.java +4 -2
  18. data/lib/commonAPI/coreapi/ext/platform/android/src/com/rho/notification/NotificationScheduler.java +3 -3
  19. data/lib/commonAPI/coreapi/ext/platform/android/src/com/rho/notification/NotificationSingleton.java +1 -1
  20. data/lib/commonAPI/coreapi/ext/push.xml +5 -2
  21. data/lib/commonAPI/mediacapture/ext/camera.xml +4 -9
  22. data/lib/commonAPI/mediacapture/ext/platform/android/ApplicationFileProvider.erb +1 -1
  23. data/lib/commonAPI/mediacapture/ext/platform/android/ext_java.files +1 -9
  24. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraExtension.java +0 -2
  25. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraFactory.java +19 -24
  26. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraObject.java +317 -729
  27. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraRhoListener.java +240 -434
  28. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/{CameraSingletonObject.java → CameraSingleton.java} +68 -74
  29. data/lib/commonAPI/printing_zebra/ext/platform/android/src/com/rhomobile/printing/zebra/impl/ZebraPrintingProviderImpl.java +2 -1
  30. data/lib/extensions/fcm-push/ext/android/ApplicationManifestAdds.erb +15 -17
  31. data/lib/extensions/fcm-push/ext/android/Rakefile +59 -34
  32. data/lib/extensions/fcm-push/ext/android/ext_java.files +0 -1
  33. data/lib/extensions/fcm-push/ext/android/src/com/rhomobile/rhodes/fcm/FCMFacade.java +13 -17
  34. data/lib/extensions/fcm-push/ext/android/src/com/rhomobile/rhodes/fcm/FCMIntentService.java +81 -80
  35. data/lib/extensions/fcm-push/ext/android/src/com/rhomobile/rhodes/fcm/FCMListener.java +0 -16
  36. data/lib/extensions/fcm-push/ext.yml +1 -1
  37. data/lib/extensions/gmaps/ext/platform/android/ApplicationManifestAdds.erb +1 -0
  38. data/lib/extensions/gmaps/ext/platform/android/src/com/rhomobile/rhodes/gmaps/GMapActivity.java +12 -4
  39. data/lib/extensions/gmaps/ext.yml +3 -1
  40. data/platform/android/Rhodes/jni/src/MethodResultJni.cpp +4 -0
  41. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesActivity.java +3 -3
  42. data/platform/android/Rhodes/src/com/rhomobile/rhodes/RhodesService.java +3 -3
  43. data/platform/android/Rhodes/src/com/rhomobile/rhodes/alert/StatusNotification.java +1 -1
  44. data/platform/android/Rhodes/src/com/rhomobile/rhodes/geolocation/GeoLocation.java +14 -14
  45. data/platform/android/Rhodes/src/com/rhomobile/rhodes/geolocation/GeoLocationImpl.java +18 -9
  46. data/platform/android/Rhodes/src/com/rhomobile/rhodes/osfunctionality/AndroidFunctionality26.java +1 -1
  47. data/platform/android/build/{aab_builder.rb → aapt2_helper.rb} +94 -37
  48. data/platform/android/build/android.rake +124 -191
  49. data/platform/android/build/android_tools.rb +9 -9
  50. data/platform/android/build/androidcommon.rb +18 -7
  51. data/platform/android/build/build_tools_finder.rb +46 -0
  52. data/platform/android/build/config.yml +5 -0
  53. data/platform/android/build/dex_builder.rb +88 -0
  54. data/platform/android/build/hostplatform.rb +9 -0
  55. data/platform/android/build/manifest_generator.rb +1 -0
  56. data/platform/android/build/maven_deps_extractor.rb +22 -21
  57. data/platform/android/build/ndkwrapper.rb +80 -51
  58. data/platform/iphone/Classes/AppManager/AppManager.m +3 -1
  59. data/platform/iphone/Classes/Camera/PickImageDelegate.h +2 -0
  60. data/platform/iphone/Classes/Camera/PickImageDelegate.m +45 -23
  61. data/platform/iphone/Classes/CocoaServer/CCocoaServer.h +27 -0
  62. data/platform/iphone/Classes/CocoaServer/CCocoaServer.m +107 -0
  63. data/platform/iphone/Classes/CocoaServer/RhoHTTPConnection.h +16 -0
  64. data/platform/iphone/Classes/CocoaServer/RhoHTTPConnection.m +226 -0
  65. data/platform/iphone/Classes/RhoWKWebView.mm +78 -11
  66. data/platform/iphone/Classes/RhoWebViewFabrique.m +6 -1
  67. data/platform/iphone/Classes/Rhodes.m +3 -0
  68. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDData.h +14 -0
  69. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDData.m +158 -0
  70. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDNumber.h +12 -0
  71. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDNumber.m +88 -0
  72. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDRange.h +56 -0
  73. data/platform/iphone/CocoaHTTPServer/Core/Categories/DDRange.m +104 -0
  74. data/platform/iphone/CocoaHTTPServer/Core/HTTPAuthenticationRequest.h +45 -0
  75. data/platform/iphone/CocoaHTTPServer/Core/HTTPAuthenticationRequest.m +195 -0
  76. data/platform/iphone/CocoaHTTPServer/Core/HTTPConnection.h +120 -0
  77. data/platform/iphone/CocoaHTTPServer/Core/HTTPConnection.m +2708 -0
  78. data/platform/iphone/CocoaHTTPServer/Core/HTTPLogging.h +136 -0
  79. data/platform/iphone/CocoaHTTPServer/Core/HTTPMessage.h +48 -0
  80. data/platform/iphone/CocoaHTTPServer/Core/HTTPMessage.m +113 -0
  81. data/platform/iphone/CocoaHTTPServer/Core/HTTPResponse.h +149 -0
  82. data/platform/iphone/CocoaHTTPServer/Core/HTTPServer.h +205 -0
  83. data/platform/iphone/CocoaHTTPServer/Core/HTTPServer.m +772 -0
  84. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartFormDataParser.h +65 -0
  85. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartFormDataParser.m +529 -0
  86. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartMessageHeader.h +33 -0
  87. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartMessageHeader.m +86 -0
  88. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartMessageHeaderField.h +23 -0
  89. data/platform/iphone/CocoaHTTPServer/Core/Mime/MultipartMessageHeaderField.m +211 -0
  90. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPAsyncFileResponse.h +75 -0
  91. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPAsyncFileResponse.m +405 -0
  92. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPDataResponse.h +13 -0
  93. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPDataResponse.m +79 -0
  94. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPDynamicFileResponse.h +52 -0
  95. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPDynamicFileResponse.m +292 -0
  96. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPErrorResponse.h +9 -0
  97. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPErrorResponse.m +38 -0
  98. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPFileResponse.h +25 -0
  99. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPFileResponse.m +237 -0
  100. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPRedirectResponse.h +12 -0
  101. data/platform/iphone/CocoaHTTPServer/Core/Responses/HTTPRedirectResponse.m +73 -0
  102. data/platform/iphone/CocoaHTTPServer/Core/WebSocket.h +105 -0
  103. data/platform/iphone/CocoaHTTPServer/Core/WebSocket.m +791 -0
  104. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DAVConnection.h +7 -0
  105. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DAVConnection.m +160 -0
  106. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DAVResponse.h +11 -0
  107. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DAVResponse.m +372 -0
  108. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DELETEResponse.h +7 -0
  109. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/DELETEResponse.m +49 -0
  110. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/PUTResponse.h +8 -0
  111. data/platform/iphone/CocoaHTTPServer/Extensions/WebDAV/PUTResponse.m +69 -0
  112. data/platform/iphone/CocoaHTTPServer/LICENSE.txt +18 -0
  113. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaAsyncSocket/About.txt +4 -0
  114. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaAsyncSocket/GCDAsyncSocket.h +1226 -0
  115. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaAsyncSocket/GCDAsyncSocket.m +8528 -0
  116. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/About.txt +33 -0
  117. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDASLLogger.h +41 -0
  118. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDASLLogger.m +99 -0
  119. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDAbstractDatabaseLogger.h +102 -0
  120. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDAbstractDatabaseLogger.m +727 -0
  121. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDFileLogger.h +334 -0
  122. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDFileLogger.m +1353 -0
  123. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDLog.h +601 -0
  124. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDLog.m +1083 -0
  125. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDTTYLogger.h +167 -0
  126. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/DDTTYLogger.m +1479 -0
  127. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/Extensions/ContextFilterLogFormatter.h +65 -0
  128. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/Extensions/ContextFilterLogFormatter.m +191 -0
  129. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/Extensions/DispatchQueueLogFormatter.h +116 -0
  130. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/Extensions/DispatchQueueLogFormatter.m +251 -0
  131. data/platform/iphone/CocoaHTTPServer/Vendor/CocoaLumberjack/Extensions/README.txt +7 -0
  132. data/platform/iphone/Framework/Rhodes/Rhodes.xcodeproj/project.pbxproj +1 -1
  133. data/platform/iphone/RhoAppBaseLib/RhoAppBaseLib.xcodeproj/project.pbxproj +364 -0
  134. data/platform/iphone/rbuild/iphone.rake +4 -0
  135. data/platform/osx/bin/RhoSimulator/RhoSimulator.app.zip +0 -0
  136. data/platform/shared/common/RhodesApp.cpp +26 -1
  137. data/platform/shared/net/HttpServer.cpp +20 -0
  138. data/platform/shared/qt/RhoSimulator.pro +1 -1
  139. data/platform/shared/ruby/osx/ruby/config.h +2 -0
  140. data/platform/shared/ruby/win32/win32.c +10 -3
  141. data/platform/win32/RhoSimulator/RhoSimulator.exe +0 -0
  142. data/rakefile.rb +52 -43
  143. data/res/build-tools/RhoRuby.exe +0 -0
  144. data/res/build-tools/aapt2/linux/aapt2 +0 -0
  145. data/res/build-tools/aapt2/osx/aapt2 +0 -0
  146. data/res/generators/templates/application/AndroidManifest.erb +2 -1
  147. data/res/generators/templates/application/build.yml +4 -1
  148. data/res/generators/templates/application/rhoconfig.txt +5 -0
  149. data/rhobuild.yml.example +4 -4
  150. data/rhodes.gemspec +1 -2
  151. data/version +1 -1
  152. metadata +80 -35
  153. data/lib/commonAPI/mediacapture/ext/platform/android/adds/res/drawable/camera_btn.xml +0 -14
  154. data/lib/commonAPI/mediacapture/ext/platform/android/adds/res/layout/camera_land.xml +0 -23
  155. data/lib/commonAPI/mediacapture/ext/platform/android/adds/res/layout/camera_port.xml +0 -23
  156. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraActivity.java +0 -156
  157. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraEclair.java +0 -227
  158. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraGingerbread.java +0 -152
  159. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraPreview.java +0 -183
  160. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraSingletonEclair.java +0 -14
  161. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/CameraSingletonGingerbread.java +0 -60
  162. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/ICameraObject.java +0 -20
  163. data/lib/commonAPI/mediacapture/ext/platform/android/src/com/rho/camera/ICameraSingletonObject.java +0 -8
  164. data/lib/extensions/fcm-push/ext/android/src/com/rhomobile/rhodes/fcm/FCMTokenRefresherService.java +0 -27
  165. data/platform/android/Rhodes/AndroidManifest.xml.erb +0 -89
@@ -0,0 +1,405 @@
1
+ #import "HTTPAsyncFileResponse.h"
2
+ #import "HTTPConnection.h"
3
+ #import "HTTPLogging.h"
4
+
5
+ #import <unistd.h>
6
+ #import <fcntl.h>
7
+
8
+ #if ! __has_feature(objc_arc)
9
+ #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
10
+ #endif
11
+
12
+ // Log levels : off, error, warn, info, verbose
13
+ // Other flags: trace
14
+ static const int httpLogLevel = HTTP_LOG_LEVEL_WARN; // | HTTP_LOG_FLAG_TRACE;
15
+
16
+ #define NULL_FD -1
17
+
18
+ /**
19
+ * Architecure overview:
20
+ *
21
+ * HTTPConnection will invoke our readDataOfLength: method to fetch data.
22
+ * We will return nil, and then proceed to read the data via our readSource on our readQueue.
23
+ * Once the requested amount of data has been read, we then pause our readSource,
24
+ * and inform the connection of the available data.
25
+ *
26
+ * While our read is in progress, we don't have to worry about the connection calling any other methods,
27
+ * except the connectionDidClose method, which would be invoked if the remote end closed the socket connection.
28
+ * To safely handle this, we do a synchronous dispatch on the readQueue,
29
+ * and nilify the connection as well as cancel our readSource.
30
+ *
31
+ * In order to minimize resource consumption during a HEAD request,
32
+ * we don't open the file until we have to (until the connection starts requesting data).
33
+ **/
34
+
35
+ @implementation HTTPAsyncFileResponse
36
+
37
+ - (id)initWithFilePath:(NSString *)fpath forConnection:(HTTPConnection *)parent
38
+ {
39
+ if ((self = [super init]))
40
+ {
41
+ HTTPLogTrace();
42
+
43
+ connection = parent; // Parents retain children, children do NOT retain parents
44
+
45
+ fileFD = NULL_FD;
46
+ filePath = [fpath copy];
47
+ if (filePath == nil)
48
+ {
49
+ HTTPLogWarn(@"%@: Init failed - Nil filePath", THIS_FILE);
50
+
51
+ return nil;
52
+ }
53
+
54
+ NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:NULL];
55
+ if (fileAttributes == nil)
56
+ {
57
+ HTTPLogWarn(@"%@: Init failed - Unable to get file attributes. filePath: %@", THIS_FILE, filePath);
58
+
59
+ return nil;
60
+ }
61
+
62
+ fileLength = (UInt64)[[fileAttributes objectForKey:NSFileSize] unsignedLongLongValue];
63
+ fileOffset = 0;
64
+
65
+ aborted = NO;
66
+
67
+ // We don't bother opening the file here.
68
+ // If this is a HEAD request we only need to know the fileLength.
69
+ }
70
+ return self;
71
+ }
72
+
73
+ - (void)abort
74
+ {
75
+ HTTPLogTrace();
76
+
77
+ [connection responseDidAbort:self];
78
+ aborted = YES;
79
+ }
80
+
81
+ - (void)processReadBuffer
82
+ {
83
+ // This method is here to allow superclasses to perform post-processing of the data.
84
+ // For an example, see the HTTPDynamicFileResponse class.
85
+ //
86
+ // At this point, the readBuffer has readBufferOffset bytes available.
87
+ // This method is in charge of updating the readBufferOffset.
88
+ // Failure to do so will cause the readBuffer to grow to fileLength. (Imagine a 1 GB file...)
89
+
90
+ // Copy the data out of the temporary readBuffer.
91
+ data = [[NSData alloc] initWithBytes:readBuffer length:readBufferOffset];
92
+
93
+ // Reset the read buffer.
94
+ readBufferOffset = 0;
95
+
96
+ // Notify the connection that we have data available for it.
97
+ [connection responseHasAvailableData:self];
98
+ }
99
+
100
+ - (void)pauseReadSource
101
+ {
102
+ if (!readSourceSuspended)
103
+ {
104
+ HTTPLogVerbose(@"%@[%p]: Suspending readSource", THIS_FILE, self);
105
+
106
+ readSourceSuspended = YES;
107
+ dispatch_suspend(readSource);
108
+ }
109
+ }
110
+
111
+ - (void)resumeReadSource
112
+ {
113
+ if (readSourceSuspended)
114
+ {
115
+ HTTPLogVerbose(@"%@[%p]: Resuming readSource", THIS_FILE, self);
116
+
117
+ readSourceSuspended = NO;
118
+ dispatch_resume(readSource);
119
+ }
120
+ }
121
+
122
+ - (void)cancelReadSource
123
+ {
124
+ HTTPLogVerbose(@"%@[%p]: Canceling readSource", THIS_FILE, self);
125
+
126
+ dispatch_source_cancel(readSource);
127
+
128
+ // Cancelling a dispatch source doesn't
129
+ // invoke the cancel handler if the dispatch source is paused.
130
+
131
+ if (readSourceSuspended)
132
+ {
133
+ readSourceSuspended = NO;
134
+ dispatch_resume(readSource);
135
+ }
136
+ }
137
+
138
+ - (BOOL)openFileAndSetupReadSource
139
+ {
140
+ HTTPLogTrace();
141
+
142
+ fileFD = open([filePath UTF8String], (O_RDONLY | O_NONBLOCK));
143
+ if (fileFD == NULL_FD)
144
+ {
145
+ HTTPLogError(@"%@: Unable to open file. filePath: %@", THIS_FILE, filePath);
146
+
147
+ return NO;
148
+ }
149
+
150
+ HTTPLogVerbose(@"%@[%p]: Open fd[%i] -> %@", THIS_FILE, self, fileFD, filePath);
151
+
152
+ readQueue = dispatch_queue_create("HTTPAsyncFileResponse", NULL);
153
+ readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fileFD, 0, readQueue);
154
+
155
+
156
+ dispatch_source_set_event_handler(readSource, ^{
157
+
158
+ HTTPLogTrace2(@"%@: eventBlock - fd[%i]", THIS_FILE, fileFD);
159
+
160
+ // Determine how much data we should read.
161
+ //
162
+ // It is OK if we ask to read more bytes than exist in the file.
163
+ // It is NOT OK to over-allocate the buffer.
164
+
165
+ unsigned long long _bytesAvailableOnFD = dispatch_source_get_data(readSource);
166
+
167
+ UInt64 _bytesLeftInFile = fileLength - readOffset;
168
+
169
+ NSUInteger bytesAvailableOnFD;
170
+ NSUInteger bytesLeftInFile;
171
+
172
+ bytesAvailableOnFD = (_bytesAvailableOnFD > NSUIntegerMax) ? NSUIntegerMax : (NSUInteger)_bytesAvailableOnFD;
173
+ bytesLeftInFile = (_bytesLeftInFile > NSUIntegerMax) ? NSUIntegerMax : (NSUInteger)_bytesLeftInFile;
174
+
175
+ NSUInteger bytesLeftInRequest = readRequestLength - readBufferOffset;
176
+
177
+ NSUInteger bytesLeft = MIN(bytesLeftInRequest, bytesLeftInFile);
178
+
179
+ NSUInteger bytesToRead = MIN(bytesAvailableOnFD, bytesLeft);
180
+
181
+ // Make sure buffer is big enough for read request.
182
+ // Do not over-allocate.
183
+
184
+ if (readBuffer == NULL || bytesToRead > (readBufferSize - readBufferOffset))
185
+ {
186
+ readBufferSize = bytesToRead;
187
+ readBuffer = reallocf(readBuffer, (size_t)bytesToRead);
188
+
189
+ if (readBuffer == NULL)
190
+ {
191
+ HTTPLogError(@"%@[%p]: Unable to allocate buffer", THIS_FILE, self);
192
+
193
+ [self pauseReadSource];
194
+ [self abort];
195
+
196
+ return;
197
+ }
198
+ }
199
+
200
+ // Perform the read
201
+
202
+ HTTPLogVerbose(@"%@[%p]: Attempting to read %lu bytes from file", THIS_FILE, self, (unsigned long)bytesToRead);
203
+
204
+ ssize_t result = read(fileFD, readBuffer + readBufferOffset, (size_t)bytesToRead);
205
+
206
+ // Check the results
207
+ if (result < 0)
208
+ {
209
+ HTTPLogError(@"%@: Error(%i) reading file(%@)", THIS_FILE, errno, filePath);
210
+
211
+ [self pauseReadSource];
212
+ [self abort];
213
+ }
214
+ else if (result == 0)
215
+ {
216
+ HTTPLogError(@"%@: Read EOF on file(%@)", THIS_FILE, filePath);
217
+
218
+ [self pauseReadSource];
219
+ [self abort];
220
+ }
221
+ else // (result > 0)
222
+ {
223
+ HTTPLogVerbose(@"%@[%p]: Read %lu bytes from file", THIS_FILE, self, (unsigned long)result);
224
+
225
+ readOffset += result;
226
+ readBufferOffset += result;
227
+
228
+ [self pauseReadSource];
229
+ [self processReadBuffer];
230
+ }
231
+
232
+ });
233
+
234
+ int theFileFD = fileFD;
235
+ #if !OS_OBJECT_USE_OBJC
236
+ dispatch_source_t theReadSource = readSource;
237
+ #endif
238
+
239
+ dispatch_source_set_cancel_handler(readSource, ^{
240
+
241
+ // Do not access self from within this block in any way, shape or form.
242
+ //
243
+ // Note: You access self if you reference an iVar.
244
+
245
+ HTTPLogTrace2(@"%@: cancelBlock - Close fd[%i]", THIS_FILE, theFileFD);
246
+
247
+ #if !OS_OBJECT_USE_OBJC
248
+ dispatch_release(theReadSource);
249
+ #endif
250
+ close(theFileFD);
251
+ });
252
+
253
+ readSourceSuspended = YES;
254
+
255
+ return YES;
256
+ }
257
+
258
+ - (BOOL)openFileIfNeeded
259
+ {
260
+ if (aborted)
261
+ {
262
+ // The file operation has been aborted.
263
+ // This could be because we failed to open the file,
264
+ // or the reading process failed.
265
+ return NO;
266
+ }
267
+
268
+ if (fileFD != NULL_FD)
269
+ {
270
+ // File has already been opened.
271
+ return YES;
272
+ }
273
+
274
+ return [self openFileAndSetupReadSource];
275
+ }
276
+
277
+ - (UInt64)contentLength
278
+ {
279
+ HTTPLogTrace2(@"%@[%p]: contentLength - %llu", THIS_FILE, self, fileLength);
280
+
281
+ return fileLength;
282
+ }
283
+
284
+ - (UInt64)offset
285
+ {
286
+ HTTPLogTrace();
287
+
288
+ return fileOffset;
289
+ }
290
+
291
+ - (void)setOffset:(UInt64)offset
292
+ {
293
+ HTTPLogTrace2(@"%@[%p]: setOffset:%llu", THIS_FILE, self, offset);
294
+
295
+ if (![self openFileIfNeeded])
296
+ {
297
+ // File opening failed,
298
+ // or response has been aborted due to another error.
299
+ return;
300
+ }
301
+
302
+ fileOffset = offset;
303
+ readOffset = offset;
304
+
305
+ off_t result = lseek(fileFD, (off_t)offset, SEEK_SET);
306
+ if (result == -1)
307
+ {
308
+ HTTPLogError(@"%@[%p]: lseek failed - errno(%i) filePath(%@)", THIS_FILE, self, errno, filePath);
309
+
310
+ [self abort];
311
+ }
312
+ }
313
+
314
+ - (NSData *)readDataOfLength:(NSUInteger)length
315
+ {
316
+ HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)length);
317
+
318
+ if (data)
319
+ {
320
+ NSUInteger dataLength = [data length];
321
+
322
+ HTTPLogVerbose(@"%@[%p]: Returning data of length %lu", THIS_FILE, self, (unsigned long)dataLength);
323
+
324
+ fileOffset += dataLength;
325
+
326
+ NSData *result = data;
327
+ data = nil;
328
+
329
+ return result;
330
+ }
331
+ else
332
+ {
333
+ if (![self openFileIfNeeded])
334
+ {
335
+ // File opening failed,
336
+ // or response has been aborted due to another error.
337
+ return nil;
338
+ }
339
+
340
+ dispatch_sync(readQueue, ^{
341
+
342
+ NSAssert(readSourceSuspended, @"Invalid logic - perhaps HTTPConnection has changed.");
343
+
344
+ readRequestLength = length;
345
+ [self resumeReadSource];
346
+ });
347
+
348
+ return nil;
349
+ }
350
+ }
351
+
352
+ - (BOOL)isDone
353
+ {
354
+ BOOL result = (fileOffset == fileLength);
355
+
356
+ HTTPLogTrace2(@"%@[%p]: isDone - %@", THIS_FILE, self, (result ? @"YES" : @"NO"));
357
+
358
+ return result;
359
+ }
360
+
361
+ - (NSString *)filePath
362
+ {
363
+ return filePath;
364
+ }
365
+
366
+ - (BOOL)isAsynchronous
367
+ {
368
+ HTTPLogTrace();
369
+
370
+ return YES;
371
+ }
372
+
373
+ - (void)connectionDidClose
374
+ {
375
+ HTTPLogTrace();
376
+
377
+ if (fileFD != NULL_FD)
378
+ {
379
+ dispatch_sync(readQueue, ^{
380
+
381
+ // Prevent any further calls to the connection
382
+ connection = nil;
383
+
384
+ // Cancel the readSource.
385
+ // We do this here because the readSource's eventBlock has retained self.
386
+ // In other words, if we don't cancel the readSource, we will never get deallocated.
387
+
388
+ [self cancelReadSource];
389
+ });
390
+ }
391
+ }
392
+
393
+ - (void)dealloc
394
+ {
395
+ HTTPLogTrace();
396
+
397
+ #if !OS_OBJECT_USE_OBJC
398
+ if (readQueue) dispatch_release(readQueue);
399
+ #endif
400
+
401
+ if (readBuffer)
402
+ free(readBuffer);
403
+ }
404
+
405
+ @end
@@ -0,0 +1,13 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import "HTTPResponse.h"
3
+
4
+
5
+ @interface HTTPDataResponse : NSObject <HTTPResponse>
6
+ {
7
+ NSUInteger offset;
8
+ NSData *data;
9
+ }
10
+
11
+ - (id)initWithData:(NSData *)data;
12
+
13
+ @end
@@ -0,0 +1,79 @@
1
+ #import "HTTPDataResponse.h"
2
+ #import "HTTPLogging.h"
3
+
4
+ #if ! __has_feature(objc_arc)
5
+ #warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
6
+ #endif
7
+
8
+ // Log levels : off, error, warn, info, verbose
9
+ // Other flags: trace
10
+ static const int httpLogLevel = HTTP_LOG_LEVEL_OFF; // | HTTP_LOG_FLAG_TRACE;
11
+
12
+
13
+ @implementation HTTPDataResponse
14
+
15
+ - (id)initWithData:(NSData *)dataParam
16
+ {
17
+ if((self = [super init]))
18
+ {
19
+ HTTPLogTrace();
20
+
21
+ offset = 0;
22
+ data = dataParam;
23
+ }
24
+ return self;
25
+ }
26
+
27
+ - (void)dealloc
28
+ {
29
+ HTTPLogTrace();
30
+
31
+ }
32
+
33
+ - (UInt64)contentLength
34
+ {
35
+ UInt64 result = (UInt64)[data length];
36
+
37
+ HTTPLogTrace2(@"%@[%p]: contentLength - %llu", THIS_FILE, self, result);
38
+
39
+ return result;
40
+ }
41
+
42
+ - (UInt64)offset
43
+ {
44
+ HTTPLogTrace();
45
+
46
+ return offset;
47
+ }
48
+
49
+ - (void)setOffset:(UInt64)offsetParam
50
+ {
51
+ HTTPLogTrace2(@"%@[%p]: setOffset:%lu", THIS_FILE, self, (unsigned long)offset);
52
+
53
+ offset = (NSUInteger)offsetParam;
54
+ }
55
+
56
+ - (NSData *)readDataOfLength:(NSUInteger)lengthParameter
57
+ {
58
+ HTTPLogTrace2(@"%@[%p]: readDataOfLength:%lu", THIS_FILE, self, (unsigned long)lengthParameter);
59
+
60
+ NSUInteger remaining = [data length] - offset;
61
+ NSUInteger length = lengthParameter < remaining ? lengthParameter : remaining;
62
+
63
+ void *bytes = (void *)([data bytes] + offset);
64
+
65
+ offset += length;
66
+
67
+ return [NSData dataWithBytesNoCopy:bytes length:length freeWhenDone:NO];
68
+ }
69
+
70
+ - (BOOL)isDone
71
+ {
72
+ BOOL result = (offset == [data length]);
73
+
74
+ HTTPLogTrace2(@"%@[%p]: isDone - %@", THIS_FILE, self, (result ? @"YES" : @"NO"));
75
+
76
+ return result;
77
+ }
78
+
79
+ @end
@@ -0,0 +1,52 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import "HTTPResponse.h"
3
+ #import "HTTPAsyncFileResponse.h"
4
+
5
+ /**
6
+ * This class is designed to assist with dynamic content.
7
+ * Imagine you have a file that you want to make dynamic:
8
+ *
9
+ * <html>
10
+ * <body>
11
+ * <h1>ComputerName Control Panel</h1>
12
+ * ...
13
+ * <li>System Time: SysTime</li>
14
+ * </body>
15
+ * </html>
16
+ *
17
+ * Now you could generate the entire file in Objective-C,
18
+ * but this would be a horribly tedious process.
19
+ * Beside, you want to design the file with professional tools to make it look pretty.
20
+ *
21
+ * So all you have to do is escape your dynamic content like this:
22
+ *
23
+ * ...
24
+ * <h1>%%ComputerName%% Control Panel</h1>
25
+ * ...
26
+ * <li>System Time: %%SysTime%%</li>
27
+ *
28
+ * And then you create an instance of this class with:
29
+ *
30
+ * - separator = @"%%"
31
+ * - replacementDictionary = { "ComputerName"="Black MacBook", "SysTime"="2010-04-30 03:18:24" }
32
+ *
33
+ * This class will then perform the replacements for you, on the fly, as it reads the file data.
34
+ * This class is also asynchronous, so it will perform the file IO using its own GCD queue.
35
+ *
36
+ * All keys for the replacementDictionary must be NSString's.
37
+ * Values for the replacementDictionary may be NSString's, or any object that
38
+ * returns what you want when its description method is invoked.
39
+ **/
40
+
41
+ @interface HTTPDynamicFileResponse : HTTPAsyncFileResponse
42
+ {
43
+ NSData *separator;
44
+ NSDictionary *replacementDict;
45
+ }
46
+
47
+ - (id)initWithFilePath:(NSString *)filePath
48
+ forConnection:(HTTPConnection *)connection
49
+ separator:(NSString *)separatorStr
50
+ replacementDictionary:(NSDictionary *)dictionary;
51
+
52
+ @end