FIRCLSMultipartMimeStreamEncoder.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <Foundation/Foundation.h>
  15. /**
  16. * This class is a helper class for generating Multipart requests, as described in
  17. * http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html. In the case of multiple part messages, in
  18. * which one or more different sets of data are combined in a single body, a "multipart"
  19. * Content-Type field must appear in the entity's header. The body must then contain one or more
  20. * "body parts," each preceded by an encapsulation boundary, and the last one followed by a closing
  21. * boundary. Each part starts with an encapsulation boundary, and then contains a body part
  22. * consisting of header area, a blank line, and a body area.
  23. */
  24. @interface FIRCLSMultipartMimeStreamEncoder : NSObject
  25. /**
  26. * Convenience class method to populate a NSMutableURLRequest with data from a block that takes an
  27. * instance of this class as input.
  28. */
  29. + (void)populateRequest:(NSMutableURLRequest *)request
  30. withDataFromEncoder:(void (^)(FIRCLSMultipartMimeStreamEncoder *encoder))block;
  31. /**
  32. * Returns a NSString instance with multipart/form-data appended to the boundary.
  33. */
  34. + (NSString *)contentTypeHTTPHeaderValueWithBoundary:(NSString *)boundary;
  35. /**
  36. * Convenience class method that returns an instance of this class
  37. */
  38. + (instancetype)encoderWithStream:(NSOutputStream *)stream andBoundary:(NSString *)boundary;
  39. /**
  40. * Returns a unique boundary string.
  41. */
  42. + (NSString *)generateBoundary;
  43. /**
  44. * Designated initializer
  45. * @param stream NSOutputStream associated with the Multipart request
  46. * @param boundary the unique Boundary string to be used
  47. */
  48. - (instancetype)initWithStream:(NSOutputStream *)stream
  49. andBoundary:(NSString *)boundary NS_DESIGNATED_INITIALIZER;
  50. - (instancetype)init NS_UNAVAILABLE;
  51. + (instancetype)new NS_UNAVAILABLE;
  52. /**
  53. * Encodes this block within the boundary on the output stream
  54. */
  55. - (void)encode:(void (^)(void))block;
  56. /**
  57. * Adds the contents of the file data with given Mime type anf fileName within the boundary in
  58. * stream
  59. */
  60. - (void)addFileData:(NSData *)data
  61. fileName:(NSString *)fileName
  62. mimeType:(NSString *)mimeType
  63. fieldName:(NSString *)name;
  64. /**
  65. * Convenience method for the method above. Converts fileURL to data and calls the above method.
  66. */
  67. - (void)addFile:(NSURL *)fileURL
  68. fileName:(NSString *)fileName
  69. mimeType:(NSString *)mimeType
  70. fieldName:(NSString *)name;
  71. /**
  72. * Adds this field and value in the stream
  73. */
  74. - (void)addValue:(id)value fieldName:(NSString *)name;
  75. /**
  76. * String referring to the multipart MIME type with boundary
  77. */
  78. @property(nonatomic, copy, readonly) NSString *contentTypeHTTPHeaderValue;
  79. /**
  80. * Length of the data written to stream
  81. */
  82. @property(nonatomic, copy, readonly) NSString *contentLengthHTTPHeaderValue;
  83. @end