@@ -447,7 +447,9 @@ - (void) bgdApply: (NSString*)basic
447447 NSMutableData *buf;
448448 NSMutableData *masked = nil ;
449449 NSString *version;
450+ NSString *method;
450451 NSMapEnumerator enumerator;
452+ NSUInteger cl;
451453
452454 RETAIN (self);
453455 if (debug)
@@ -501,15 +503,48 @@ - (void) bgdApply: (NSString*)basic
501503 }
502504 }
503505
504- /* Ensure we set the correct content length (may be zero)
506+ cl = [wData length ];
507+
508+ /* Use default method if none is set.
509+ */
510+ method = [request objectForKey: GSHTTPPropertyMethodKey];
511+ if (method == nil )
512+ {
513+ if (cl > 0 )
514+ {
515+ method = @" POST" ;
516+ }
517+ else
518+ {
519+ method = @" GET" ;
520+ }
521+ }
522+ else if ([method isEqualToString: @" TRACE" ])
523+ {
524+ /* A TRACE must not have a body
525+ */
526+ DESTROY (wData);
527+ cl = 0 ;
528+ NSMapRemove (wProperties, (void *)@" Content-Length" );
529+ }
530+
531+
532+ /* When we have a non-empty body or a method which requires
533+ * a body, we must specify the content length.
505534 */
506535 if ((id )NSMapGet (wProperties, (void *)@" Content-Length" ) == nil )
507536 {
508- NSMapInsert (wProperties, (void *)@" Content-Length" ,
509- (void *)[NSString stringWithFormat: @" %" PRIuPTR, [wData length ]]);
537+ if (cl > 0
538+ || [method isEqualToString: @" POST" ]
539+ || [method isEqualToString: @" PUT" ]
540+ || [method isEqualToString: @" PATCH" ])
541+ {
542+ NSMapInsert (wProperties, (void *)@" Content-Length" ,
543+ (void *)[NSString stringWithFormat: @" %" PRIuPTR, cl]);
544+ }
510545 }
511546
512- if ([wData length ] > 0 )
547+ if (cl > 0 )
513548 {
514549 /*
515550 * Assume content type if not specified.
@@ -536,7 +571,6 @@ - (void) bgdApply: (NSString*)basic
536571 NSString *auth;
537572 GSHTTPAuthentication *authentication;
538573 NSURLCredential *cred;
539- NSString *method;
540574 NSString *path;
541575
542576 /* Create credential from user and password stored in the URL.
@@ -562,19 +596,6 @@ - (void) bgdApply: (NSString*)basic
562596 RELEASE (cred);
563597 }
564598
565- method = [request objectForKey: GSHTTPPropertyMethodKey];
566- if (method == nil )
567- {
568- if ([wData length ] > 0 )
569- {
570- method = @" POST" ;
571- }
572- else
573- {
574- method = @" GET" ;
575- }
576- }
577-
578599 path = [u _requestPath: [[request
579600 objectForKey: GSHTTPPropertyDigestURIOmitsQuery] boolValue ]];
580601
0 commit comments