@@ -156,7 +156,7 @@ app.get('/access-client', function(req, res) {
156156} ) ;
157157
158158
159- /** ACCESS CORS */
159+ /** AUTHORIZATION CORS */
160160app . get ( '/amp-authorization.json' , function ( req , res ) {
161161 console . log ( 'Client access verification' ) ;
162162 var readerId = req . query . rid ;
@@ -200,6 +200,39 @@ app.get('/amp-authorization.json', function(req, res) {
200200} ) ;
201201
202202
203+ /** PINGBACK CORS */
204+ app . post ( '/amp-pingback' , function ( req , res ) {
205+ console . log ( 'Client access pingback' ) ;
206+ var readerId = req . query . rid ;
207+ if ( ! readerId ) {
208+ res . sendStatus ( 400 ) ;
209+ return ;
210+ }
211+
212+ // In practice, Origin should be restricted to a few well-known domains.
213+ var requestingOrigin = req . header ( 'Origin' ) ;
214+ console . log ( '---- requesting origin: ' , requestingOrigin ) ;
215+ if ( requestingOrigin ) {
216+ res . setHeader ( 'Access-Control-Allow-Origin' , requestingOrigin ) ;
217+ }
218+ res . setHeader ( 'Access-Control-Allow-Credentials' , 'true' ) ;
219+
220+ var clientAuth = CLIENT_ACCESS [ readerId ] ;
221+ if ( ! clientAuth ) {
222+ clientAuth = { } ;
223+ CLIENT_ACCESS [ readerId ] = clientAuth ;
224+ }
225+
226+ if ( ! clientAuth . subscriber ) {
227+ // Metered.
228+ var views = ( clientAuth . views || 0 ) + 1 ;
229+ clientAuth . views = views ;
230+ }
231+ console . log ( 'Pingback response:' , readerId , { } , clientAuth ) ;
232+ res . json ( { } ) ;
233+ } ) ;
234+
235+
203236var server = app . listen ( PORT , function ( ) {
204237 var host = server . address ( ) . address ;
205238 var port = server . address ( ) . port ;
0 commit comments