@@ -28,6 +28,7 @@ public class TusClient {
2828 private boolean addRequestId ;
2929 private TusURLStore urlStore ;
3030 private Map <String , String > headers ;
31+ private String protocol = TusProtocol .DEFAULT_CLIENT_PROTOCOL ;
3132 private int connectTimeout = 5000 ;
3233 private TusRequestLifecycleHooks requestLifecycleHooks ;
3334 private volatile HttpURLConnection currentConnection ;
@@ -169,6 +170,31 @@ public Map<String, String> getHeaders() {
169170 return headers ;
170171 }
171172
173+ /**
174+ * Select the TUS client protocol mode used for generated protocol headers.
175+ *
176+ * @param protocol The protocol mode, e.g. {@code tus-v1} or {@code ietf-draft-05}.
177+ */
178+ public void setProtocol (@ Nullable String protocol ) {
179+ final String normalizedProtocol = TusProtocol .normalizeProtocol (protocol );
180+ if (normalizedProtocol == null ) {
181+ throw new IllegalArgumentException (
182+ TusProtocol .START_OPTION_VALIDATION_UNSUPPORTED_PROTOCOL_PREFIX + protocol
183+ );
184+ }
185+
186+ this .protocol = normalizedProtocol ;
187+ }
188+
189+ /**
190+ * Return the selected TUS client protocol mode.
191+ *
192+ * @return The selected protocol mode.
193+ */
194+ public String getProtocol () {
195+ return protocol ;
196+ }
197+
172198 /**
173199 * Enable generated request IDs for every HTTP request made by this TusClient instance.
174200 */
@@ -363,10 +389,7 @@ private TusUploader createUpload(
363389 prepareUploadCreationHeaders (connection , upload );
364390
365391 if (bytesToUpload > 0 ) {
366- connection .setRequestProperty (
367- TusProtocol .UPLOAD_BODY_CONTENT_TYPE_HEADER_NAME ,
368- TusProtocol .UPLOAD_BODY_CONTENT_TYPE
369- );
392+ prepareUploadBodyHeaders (connection , bytesToUpload >= upload .getSize ());
370393 connection .setDoOutput (true );
371394 connection .setFixedLengthStreamingMode (bytesToUpload );
372395 }
@@ -535,6 +558,30 @@ private static void prepareUploadMetadataHeaders(
535558 }
536559 }
537560
561+ private void prepareUploadBodyHeaders (
562+ @ NotNull HttpURLConnection connection ,
563+ boolean requestCompletesUpload
564+ ) {
565+ final String contentType = TusProtocol .protocolUploadBodyContentType (protocol );
566+ if (contentType != null ) {
567+ connection .setRequestProperty (
568+ TusProtocol .UPLOAD_BODY_CONTENT_TYPE_HEADER_NAME ,
569+ contentType
570+ );
571+ }
572+
573+ final String uploadCompleteHeaderName =
574+ TusProtocol .protocolUploadCompleteHeaderName (protocol );
575+ if (uploadCompleteHeaderName == null ) {
576+ return ;
577+ }
578+
579+ connection .setRequestProperty (
580+ uploadCompleteHeaderName ,
581+ TusProtocol .protocolUploadCompleteHeaderValue (protocol , requestCompletesUpload )
582+ );
583+ }
584+
538585 private static String finalUploadConcatValue (@ NotNull List <URL > uploadURLs ) {
539586 StringBuilder value = new StringBuilder (TusProtocol .CONCATENATION_FINAL_PREFIX );
540587 for (int index = 0 ; index < uploadURLs .size (); index ++) {
@@ -814,7 +861,7 @@ public void prepareConnection(@NotNull HttpURLConnection connection) {
814861 connection .setInstanceFollowRedirects (Boolean .getBoolean ("http.strictPostRedirect" ));
815862
816863 connection .setConnectTimeout (connectTimeout );
817- TusProtocol .prepareRequestHeaders (connection , headers , addRequestId );
864+ TusProtocol .prepareRequestHeaders (connection , headers , addRequestId , protocol );
818865 }
819866
820867 final void runBeforeRequest (
0 commit comments