@@ -19,9 +19,21 @@ function endWith($haystack, $needle) {
1919class BucketStyleListener implements EventSubscriberInterface {
2020
2121 private $ appId ; // string: application id.
22+ private $ ip ;
23+ private $ port ;
24+ private $ ipport ;
2225
23- public function __construct ($ appId ) {
26+ public function __construct ($ appId, $ ip = null , $ port = null ) {
2427 $ this ->appId = $ appId ;
28+ $ this ->ip = $ ip ;
29+ $ this ->port = $ port ;
30+ $ this ->ipport = null ;
31+ if ($ ip != null ) {
32+ $ this ->ipport = $ ip ;
33+ if ($ port != null ) {
34+ $ this ->ipport = $ ip .": " .$ port ;
35+ }
36+ }
2537 }
2638
2739 public static function getSubscribedEvents () {
@@ -33,13 +45,19 @@ public static function getSubscribedEvents() {
3345 * @param Event $event Event emitted.
3446 */
3547 public function onCommandAfterPrepare (Event $ event ) {
48+
3649 $ command = $ event ['command ' ];
3750 $ bucket = $ command ['Bucket ' ];
3851 $ request = $ command ->getRequest ();
39-
4052 if ($ command ->getName () == 'ListBuckets ' )
4153 {
42- $ request ->setHost ('service.cos.myqcloud.com ' );
54+ if ($ this ->ipport != null ) {
55+ $ request ->setHost ($ this ->ipport );
56+ $ request ->setHeader ('Host ' , 'service.cos.myqcloud.com ' );
57+ } else {
58+
59+ $ request ->setHost ('service.cos.myqcloud.com ' );
60+ }
4361 return ;
4462 }
4563 if ($ key = $ command ['Key ' ]) {
@@ -55,12 +73,16 @@ public function onCommandAfterPrepare(Event $event) {
5573 {
5674 $ bucket = $ bucket .'- ' .$ this ->appId ;
5775 }
58- // Set the key and bucket on the request
76+ $ request-> setPath ( urldecode ( $ request -> getPath ()));
5977 $ request ->getParams ()->set ('bucket ' , $ bucket )->set ('key ' , $ key );
6078
61- //$request->setPath(urldecode($request->getPath()));
62- // Switch to virtual hosted bucket
63- $ request ->setHost ($ bucket . '. ' . $ request ->getHost ());
79+ $ realHost = $ bucket . '. ' . $ request ->getHost ();
80+ if ($ this ->ipport != null ) {
81+ $ request ->setHost ($ this ->ipport );
82+ $ request ->setHeader ('Host ' , $ realHost );
83+ } else {
84+ $ request ->setHost ($ realHost );
85+ }
6486 if (!$ bucket ) {
6587 $ request ->getParams ()->set ('cos.resource ' , '/ ' );
6688 } else {
0 commit comments