@@ -59,11 +59,6 @@ class HttpSocket extends CakeSocket {
59
59
'query ' => null ,
60
60
'fragment ' => null
61
61
),
62
- 'auth ' => array (
63
- 'method ' => 'Basic ' ,
64
- 'user ' => null ,
65
- 'pass ' => null
66
- ),
67
62
'proxy ' => array (
68
63
'method ' => 'Basic ' ,
69
64
'host ' => null ,
@@ -123,11 +118,6 @@ class HttpSocket extends CakeSocket {
123
118
'host ' => 'localhost ' ,
124
119
'port ' => 80
125
120
),
126
- 'auth ' => array (
127
- 'method ' => 'Basic ' ,
128
- 'user ' => null ,
129
- 'pass ' => null
130
- ),
131
121
'proxy ' => array (
132
122
'method ' => 'Basic ' ,
133
123
'host ' => null ,
@@ -147,6 +137,14 @@ class HttpSocket extends CakeSocket {
147
137
*/
148
138
public $ lineBreak = "\r\n" ;
149
139
140
+ /**
141
+ * Authentication settings
142
+ *
143
+ * @var array
144
+ * @access protected
145
+ */
146
+ protected $ _auth = array ();
147
+
150
148
/**
151
149
* Build an HTTP Socket using the specified configuration.
152
150
*
@@ -181,6 +179,26 @@ public function __construct($config = array()) {
181
179
parent ::__construct ($ this ->config );
182
180
}
183
181
182
+ /**
183
+ * Set authentication settings
184
+ *
185
+ * @param string $method Authentication method (ex. Basic, Digest). If empty, disable authentication
186
+ * @param mixed $user Username for authentication. Can be an array with settings to authentication class
187
+ * @param string $pass Password for authentication
188
+ * @return void
189
+ */
190
+ public function setAuthConfig ($ method , $ user , $ pass = null ) {
191
+ if (empty ($ method )) {
192
+ $ this ->_auth = array ();
193
+ return ;
194
+ }
195
+ if (is_array ($ user )) {
196
+ $ this ->_auth = array ($ method => $ user );
197
+ return ;
198
+ }
199
+ $ this ->_auth = array ($ method => compact ('user ' , 'pass ' ));
200
+ }
201
+
184
202
/**
185
203
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
186
204
* method and provide a more granular interface.
@@ -201,10 +219,6 @@ public function request($request = array()) {
201
219
$ request ['uri ' ] = null ;
202
220
}
203
221
$ uri = $ this ->_parseUri ($ request ['uri ' ]);
204
- $ hadAuth = false ;
205
- if (is_array ($ uri ) && array_key_exists ('user ' , $ uri )) {
206
- $ hadAuth = true ;
207
- }
208
222
if (!isset ($ uri ['host ' ])) {
209
223
$ host = $ this ->config ['host ' ];
210
224
}
@@ -216,10 +230,6 @@ public function request($request = array()) {
216
230
$ request ['uri ' ] = $ this ->_parseUri ($ request ['uri ' ], true );
217
231
$ this ->request = Set::merge ($ this ->request , $ this ->config ['request ' ], $ request );
218
232
219
- if (!$ hadAuth && !empty ($ this ->config ['request ' ]['auth ' ]['user ' ])) {
220
- $ this ->request ['uri ' ]['user ' ] = $ this ->config ['request ' ]['auth ' ]['user ' ];
221
- $ this ->request ['uri ' ]['pass ' ] = $ this ->config ['request ' ]['auth ' ]['pass ' ];
222
- }
223
233
$ this ->_configUri ($ this ->request ['uri ' ]);
224
234
225
235
if (isset ($ host )) {
@@ -251,6 +261,9 @@ public function request($request = array()) {
251
261
$ this ->request ['header ' ] = array_merge (compact ('Host ' ), $ this ->request ['header ' ]);
252
262
}
253
263
264
+ if (isset ($ this ->request ['uri ' ]['user ' ], $ this ->request ['uri ' ]['pass ' ])) {
265
+ $ this ->setAuthConfig ('Basic ' , $ this ->request ['uri ' ]['user ' ], $ this ->request ['uri ' ]['pass ' ]);
266
+ }
254
267
$ this ->_setAuth ();
255
268
$ this ->_setProxyConfig ();
256
269
@@ -457,28 +470,18 @@ public function url($url = null, $uriTemplate = null) {
457
470
* @throws Exception
458
471
*/
459
472
protected function _setAuth () {
460
- if ($ this ->request [ ' auth ' ][ ' method ' ] === false ) {
473
+ if (empty ( $ this ->_auth ) ) {
461
474
return ;
462
475
}
463
- if (empty ($ this ->request ['auth ' ]['method ' ])) {
464
- if (isset ($ this ->request ['uri ' ]['user ' ], $ this ->request ['uri ' ]['pass ' ]) && !isset ($ this ->request ['auth ' ]['user ' ])) {
465
- $ this ->request ['auth ' ] = array (
466
- 'method ' => 'Basic ' ,
467
- 'user ' => $ this ->request ['uri ' ]['user ' ],
468
- 'pass ' => $ this ->request ['uri ' ]['pass ' ]
469
- );
470
- } else {
471
- return ;
472
- }
473
- }
474
- $ authClass = Inflector::camelize ($ this ->request ['auth ' ]['method ' ]) . 'Authentication ' ;
476
+ $ method = key ($ this ->_auth );
477
+ $ authClass = Inflector::camelize ($ method ) . 'Authentication ' ;
475
478
if (!App::import ('Lib ' , 'http/ ' . $ authClass )) {
476
479
throw new Exception (__ ('Unknown authentication method. ' ));
477
480
}
478
481
if (!method_exists ($ authClass , 'authentication ' )) {
479
482
throw new Exception (sprintf (__ ('The %s do not support authentication. ' ), $ authClass ));
480
483
}
481
- call_user_func ("$ authClass::authentication " , $ this );
484
+ call_user_func ("$ authClass::authentication " , $ this , & $ this -> _auth [ $ method ] );
482
485
}
483
486
484
487
/**
@@ -677,8 +680,7 @@ protected function _configUri($uri = null) {
677
680
}
678
681
$ config = array (
679
682
'request ' => array (
680
- 'uri ' => array_intersect_key ($ uri , $ this ->config ['request ' ]['uri ' ]),
681
- 'auth ' => array_intersect_key ($ uri , $ this ->config ['request ' ]['auth ' ])
683
+ 'uri ' => array_intersect_key ($ uri , $ this ->config ['request ' ]['uri ' ])
682
684
)
683
685
);
684
686
$ this ->config = Set::merge ($ this ->config , $ config );
0 commit comments