forked from gronke/polygit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.php
199 lines (165 loc) · 4.4 KB
/
magic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
// you must provide `config.php` which defines
// $github_token = <GITHUB API TOKEN>
require_once('config.php');
// use a memcache to memoize github API (version) data
if (defined('Memcache')) {
$mem = new Memcache;
} else {
class cacheStub {
function get() {
return null;
}
function set() {
}
}
$mem = new cacheStub();
}
// support CORS
header('Access-Control-Allow-Origin: *');
header("polygit-hello: hello");
// grab up the money bits of the url
$path = $_SERVER['REQUEST_URI'];
$parts = explode('/', $path);
$ORG = '';
$VERSION = '';
// extract component path
$path = array();
while (count($parts)) {
$part = array_pop($parts);
if ($part === 'components') {
break;
}
array_unshift($path, $part);
}
$COMPONENT = array_shift($path);
$IMPORT = implode('/', $path);
// snarf up config from rest of url
$CONFIG = array();
$l = count($parts);
while ($l--) {
$data = array_pop($parts);
//echo "[$data]<br>";
$data = explode('+', $data);
//echo "<pre>";
//print_r($data);
//echo "</pre>";
$ver = array_pop($data);
$org = (count($data) > 1) ? array_pop($data) : '';
$comp = array_pop($data);
//echo "testing [$comp] against [$COMPONENT]<br>";
// shell wildcard matching
if (fnmatch($comp, $COMPONENT)) {
//echo "matched [$comp] to [$COMPONENT]<br>";
$VERSION = $ver;
$ORG = $org;
break;
}
}
// try to determine $ORG if not configured
if (!$ORG) {
if ($COMPONENT == 'polymer' || $COMPONENT == 'hydrolysis' || $COMPONENT == 'web-component-tester') {
$ORG = 'Polymer';
} else if ($COMPONENT == 'promise-polyfill') {
$ORG = 'PolymerLabs';
} else if ($COMPONENT == 'marked' || $COMPONENT == 'prism' || $COMPONENT == 'iron-component-page') {
$ORG = 'sjmiles';
} else if (prefix($COMPONENT, 'webcomponents')) {
$ORG = 'webcomponents';
} else if (prefix($COMPONENT, 'web-animations-js')) {
$ORG = 'web-animations';
} else if (prefix($COMPONENT, 'google-')) {
$ORG = 'GoogleWebComponents';
} else {
$ORG = 'PolymerElements';
}
}
//
// desugar $VERSION
//
if (@$VERSION{0} === ':') {
$BRANCH = substr($VERSION, 1);
$info = github("repos/$ORG/$COMPONENT/git/refs/heads/$BRANCH");
$VERSION = $info->object->sha;
}
if (!$VERSION || $VERSION == '*') {
// most recent RELEASE
$info = github("repos/$ORG/$COMPONENT/releases/latest");
$VERSION = $info->tag_name;
}
if (!$VERSION) {
$BRANCH = 'master';
$info = github("repos/$ORG/$COMPONENT/git/refs/heads/$BRANCH");
$VERSION = $info->object->sha;
header("polygit-version: defaulting to [:master]");
}
// supply info
header("polygit-url: $ORG/$COMPONENT/$VERSION/$IMPORT");
//
// proxy actual bytes from cdn.rawgit
//
$data = rawgit("$ORG/$COMPONENT/$VERSION/$IMPORT");
// parse the response headers
$res = parseHeaders($rawgit_response);
// handle 404
if ($res['code'] == 404) {
header($res[0]);
echo "<!-- $ORG/$COMPONENT/$VERSION/$IMPORT -->\n";
} else {
// forward content-type
$type = $res['Content-Type'];
header("Content-Type: $type");
// forward byte stream
echo $data;
}
//
// utilities
//
function github($url) {
global $github_token, $git_response, $mem;
//
$contents = $mem->get($url);
//
if (!$contents) {
$cmd = "https://api.github.com/$url?access_token=$github_token";
//
$contents = file_get_contents($cmd, false,
stream_context_create(array('http'=>array('header'=>"User-Agent: Polymer-Magic-Server\r\n"))));
$github_response = $http_response_header;
//
$mem->set($url, $contents, 0, 60*60);
//
header("polygit-github-info: lookup [$url]");
} else {
header("polygit-github-info: cached [$url]");
}
//
return json_decode($contents);
}
function rawgit($path) {
global $rawgit_response;
//
$contents = file_get_contents("https://cdn.rawgit.com/$path", false,
stream_context_create(array('http'=>array('header'=>"User-Agent: Polymer-Magic-Server\r\n"))));
$rawgit_response = $http_response_header;
//
return $contents;
}
function prefix($haystack, $needle) {
return strncmp($haystack, $needle, strlen($needle)) === 0;
}
function parseHeaders($headers) {
$head = array();
foreach( $headers as $k=>$v ) {
$t = explode( ':', $v, 2 );
if(isset($t[1])) {
$head[ trim($t[0]) ] = trim( $t[1] );
} else {
$head[] = $v;
if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) )
$head['code'] = intval($out[1]);
}
}
return $head;
}
?>