Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
solarium
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
solarium
Commits
0b2b8351
Commit
0b2b8351
authored
Aug 18, 2011
by
Gasol Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use pecl HTTP classes instead of functions
parent
bd44a93a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
53 deletions
+75
-53
library/Solarium/Client/Adapter/PeclHttp.php
library/Solarium/Client/Adapter/PeclHttp.php
+75
-53
No files found.
library/Solarium/Client/Adapter/PeclHttp.php
View file @
0b2b8351
...
@@ -71,53 +71,88 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
...
@@ -71,53 +71,88 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
*/
*/
public
function
execute
(
$request
)
public
function
execute
(
$request
)
{
{
list
(
$data
,
$headers
)
=
$this
->
_getData
(
$request
);
$httpRequest
=
$this
->
getHttpRequest
(
$request
);
$this
->
check
(
$data
,
$headers
);
return
new
Solarium_Client_Response
(
$data
,
$headers
);
try
{
$httpMessage
=
$httpRequest
->
send
();
}
catch
(
Exception
$e
)
{
throw
new
Solarium_Client_HttpException
(
$e
->
getMessage
());
}
if
(
$error
=
$httpRequest
->
getResponseInfo
(
'error'
))
{
throw
new
Solarium_Client_HttpException
(
$error
);
}
return
new
Solarium_Client_Response
(
$httpMessage
->
getBody
(),
$this
->
_getRawHeaders
(
$httpMessage
));
}
}
/**
/**
*
Execute request
*
Convert key/value pair header to raw header.
*
*
* @param Solarium_Client_Request $request
* <code>
* //before
* $headers['Content-Type'] = 'text/plain';
*
* ...
*
* //after
* $headers[0] = 'Content-Type: text/plain';
* </code>
*
* @param $message HttpMessage
* @return array
* @return array
*/
*/
protected
function
_get
Data
(
$request
)
protected
function
_get
RawHeaders
(
$message
)
{
{
// @codeCoverageIgnoreStart
$headers
[]
=
'HTTP/'
.
$message
->
getHttpVersion
()
$uri
=
$this
->
getBaseUri
()
.
$request
->
getUri
();
.
' '
.
$message
->
getResponseCode
()
$method
=
$request
->
getMethod
();
.
' '
.
$message
->
getResponseStatus
();
$options
=
$this
->
_createOptions
(
$request
);
if
(
$method
==
Solarium_Client_Request
::
METHOD_POST
)
{
foreach
(
$message
->
getHeaders
()
as
$header
=>
$value
)
{
if
(
!
isset
(
$options
[
'headers'
][
'Content-Type'
]))
{
$headers
[]
=
"
$header
:
$value
"
;
$options
[
'headers'
][
'Content-Type'
]
=
'text/xml; charset=utf-8'
;
}
}
$httpResponse
=
http_post_data
(
$uri
,
$request
->
getRawData
(),
$options
return
$headers
;
);
}
else
if
(
$method
==
Solarium_Client_Request
::
METHOD_GET
)
{
$httpResponse
=
http_get
(
$uri
,
$options
);
}
else
if
(
$method
==
Solarium_Client_Request
::
METHOD_HEAD
)
{
$httpResponse
=
http_head
(
$uri
,
$options
);
}
else
{
throw
new
Solarium_Exception
(
"unsupported method:
$method
"
);
}
}
$headers
=
array
();
/**
$data
=
''
;
*
if
(
$message
=
http_parse_message
(
$httpResponse
))
{
* adapt Solarium_Client_Request to HttpRequest
$data
=
$message
->
body
;
*
if
(
$firstPositionOfCRLF
=
strpos
(
$httpResponse
,
"
\r\n\r\n
"
))
{
* {@link http://us.php.net/manual/en/http.constants.php
$headersAsString
=
substr
(
* HTTP Predefined Constant}
$httpResponse
,
0
,
$firstPositionOfCRLF
*
);
* @param Solarium_Client_Request $request
$headers
=
explode
(
"
\n
"
,
$headersAsString
);
* @param HttpRequest
*/
protected
function
getHttpRequest
(
$request
)
{
$url
=
$this
->
getBaseUri
()
.
$request
->
getUri
();
switch
(
$request
->
getMethod
())
{
case
Solarium_Client_Request
::
METHOD_GET
:
$method
=
HTTP_METH_GET
;
break
;
case
Solarium_Client_Request
::
METHOD_POST
:
$method
=
HTTP_METH_POST
;
break
;
case
Solarium_Client_Request
::
METHOD_HEAD
:
$method
=
HTTP_METH_HEAD
;
break
;
default
:
throw
new
Solarium_Exception
(
'Unsupported method: '
.
$request
->
getMethod
());
}
}
$options
=
$this
->
_createOptions
(
$request
);
$httpRequest
=
new
HttpRequest
(
$url
,
$method
,
$options
);
if
(
HTTP_METH_POST
==
$httpRequest
->
getMethod
())
{
$httpRequest
->
setBody
(
$request
->
getRawData
());
}
}
return
array
(
$data
,
$headers
);
return
$httpRequest
;
// @codeCoverageIgnoreEnd
}
}
/**
/**
...
@@ -140,24 +175,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
...
@@ -140,24 +175,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
$options
[
'headers'
][
$header
]
=
trim
(
$value
);
$options
[
'headers'
][
$header
]
=
trim
(
$value
);
}
}
}
}
return
$options
;
if
(
$request
->
getMethod
()
==
Solarium_Client_Request
::
METHOD_POST
)
{
// @codeCoverageIgnoreEnd
if
(
!
isset
(
$options
[
'headers'
][
'Content-Type'
]))
{
$options
[
'headers'
][
'Content-Type'
]
=
'text/xml; charset=utf-8'
;
}
}
/**
* Check result of a request
*
* @throws Solarium_Client_HttpException
* @param string $data
* @param array $headers
* @return void
*/
public
function
check
(
$data
,
$headers
)
{
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if
(
empty
(
$data
)
&&
count
(
$headers
)
==
0
)
{
throw
new
Solarium_Client_HttpException
(
"HTTP request failed"
);
}
}
return
$options
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment