This repository was archived by the owner on Jun 27, 2018. It is now read-only.
-
Couldn't load subscription status.
- Fork 74
Added support for using custom HTTP clients #12
Open
jeremeamia
wants to merge
1
commit into
1EdTech:master
Choose a base branch
from
jeremeamia:feature/allow-custom-http-clients
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # EditorConfig is awesome: http://EditorConfig.org | ||
|
|
||
| # top-most EditorConfig file | ||
| root = true | ||
|
|
||
| # Unix-style newlines with a newline ending for every file | ||
| # Indent with 4 spaces | ||
| [php] | ||
| end_of_line = lf | ||
| indent_style = space | ||
| indent_size = 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| phpunit.xml | ||
| composer.phar | ||
| composer.lock | ||
| vendor/ | ||
| build/ | ||
| .idea | ||
| .DS_STORE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit bootstrap="./vendor/autoload.php" | ||
| forceCoversAnnotation="true"> | ||
|
|
||
| <testsuites> | ||
| <testsuite> | ||
| <directory suffix="Test.php">tests/</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| <filter> | ||
| <whitelist addUncoveredFilesFromWhitelist="true"> | ||
| <directory suffix=".php">src</directory> | ||
| </whitelist> | ||
| </filter> | ||
|
|
||
| <php> | ||
| <server name="TEST_SERVER_PORT" value="9999"/> | ||
| </php> | ||
|
|
||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace IMSGlobal\LTI\HTTP; | ||
|
|
||
| use IMSGlobal\LTI\HTTPMessage; | ||
|
|
||
| /** | ||
| * An HTTP client for sending the HTTP messages. | ||
| * | ||
| * @author Stephen P Vickers <[email protected]> | ||
| * @copyright IMS Global Learning Consortium Inc | ||
| * @date 2016 | ||
| * @version 3.0.0 | ||
| * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 | ||
| */ | ||
| interface Client | ||
| { | ||
|
|
||
| /** | ||
| * Send the provided HTTPMessage and then updates it with the response data. | ||
| * | ||
| * @param HTTPMessage $message The HTTP message to send | ||
| * @return bool If successful, returns true | ||
| */ | ||
| public function send(HTTPMessage $message); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| namespace IMSGlobal\LTI\HTTP; | ||
|
|
||
| use IMSGlobal\LTI\HTTPMessage; | ||
|
|
||
| /** | ||
| * Sends HTTP messages with cURL. | ||
| * | ||
| * @author Stephen P Vickers <[email protected]> | ||
| * @copyright IMS Global Learning Consortium Inc | ||
| * @date 2016 | ||
| * @version 3.0.0 | ||
| * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 | ||
| */ | ||
| class CurlClient implements Client | ||
| { | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function send(HTTPMessage $message) | ||
| { | ||
| $message->ok = false; | ||
|
|
||
| $resp = ''; | ||
| $ch = curl_init(); | ||
| curl_setopt($ch, CURLOPT_URL, $message->url); | ||
| if (!empty($message->requestHeaders)) { | ||
| curl_setopt($ch, CURLOPT_HTTPHEADER, $message->requestHeaders); | ||
| } else { | ||
| curl_setopt($ch, CURLOPT_HEADER, 0); | ||
| } | ||
| if ($message->method === 'POST') { | ||
| curl_setopt($ch, CURLOPT_POST, true); | ||
| curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request); | ||
| } else if ($message->method !== 'GET') { | ||
| curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $message->method); | ||
| if (!is_null($message->request)) { | ||
| curl_setopt($ch, CURLOPT_POSTFIELDS, $message->request); | ||
| } | ||
| } | ||
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| curl_setopt($ch, CURLINFO_HEADER_OUT, true); | ||
| curl_setopt($ch, CURLOPT_HEADER, true); | ||
| $chResp = curl_exec($ch); | ||
| $message->ok = $chResp !== false; | ||
| if ($message->ok) { | ||
| $chResp = str_replace("\r\n", "\n", $chResp); | ||
| $chRespSplit = explode("\n\n", $chResp, 2); | ||
| if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) === 'HTTP/')) { | ||
| $chRespSplit = explode("\n\n", $chRespSplit[1], 2); | ||
| } | ||
| $message->responseHeaders = $chRespSplit[0]; | ||
| $resp = $chRespSplit[1]; | ||
| $message->status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| $message->ok = $message->status < 400; | ||
| if (!$message->ok) { | ||
| $message->error = curl_error($ch); | ||
| } | ||
| } | ||
| $message->requestHeaders = str_replace("\r\n", "\n", curl_getinfo($ch, CURLINFO_HEADER_OUT)); | ||
| curl_close($ch); | ||
| $message->response = $resp; | ||
|
|
||
| return $message->ok; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| namespace IMSGlobal\LTI\HTTP; | ||
|
|
||
| use IMSGlobal\LTI\HTTPMessage; | ||
|
|
||
| /** | ||
| * Sends HTTP messages with streams via fopen. | ||
| * | ||
| * @author Stephen P Vickers <[email protected]> | ||
| * @copyright IMS Global Learning Consortium Inc | ||
| * @date 2016 | ||
| * @version 3.0.0 | ||
| * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 | ||
| */ | ||
| class StreamClient implements Client | ||
| { | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function send(HTTPMessage $message) | ||
| { | ||
| $message->ok = false; | ||
|
|
||
| // Prepare options for the HTTP context. | ||
| $opts = array( | ||
| 'method' => $message->method, | ||
| 'content' => $message->request | ||
| ); | ||
| if (!empty($message->requestHeaders)) { | ||
| $opts['header'] = $message->requestHeaders; | ||
| } | ||
|
|
||
| // Send the request. | ||
| $http_response_header = null; | ||
| $context = stream_context_create(['http' => $opts]); | ||
| $stream = @fopen($message->url, 'rb', false, $context); | ||
| if ($stream) { | ||
| $message->response = @stream_get_contents($stream); | ||
| fclose($stream); | ||
| } | ||
|
|
||
| // Read the headers to get the status. | ||
| if ($http_response_header) { | ||
| $message->responseHeaders = implode("\n", $http_response_header); | ||
| $parts = explode(' ', $message->responseHeaders, 3); | ||
| $message->status = $parts[1]; | ||
| $message->ok = $message->status < 400; | ||
| } | ||
|
|
||
| return $message->ok; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this behavior originally included?