11<?php
22ob_start ();
33
4- define ('SESSION_FILE ' , '/tmp/session.dat ' );
5-
6- if (file_exists (SESSION_FILE )) {
7- $ session = unserialize (file_get_contents (SESSION_FILE ));
8- } else {
9- $ session = array ('Compression-Count ' => 0 );
10- }
4+ require_once ('common.php ' );
115
126function mock_png_response () {
137 global $ session ;
148
159 $ session ['Compression-Count ' ] += 1 ;
1610 header ('HTTP/1.1 201 Created ' );
1711 header ("Location: http://webservice/output/example.png " );
12+ header ("Content-Type: application/json; charset=utf-8 " );
1813 header ("Compression-Count: {$ session ['Compression-Count ' ]}" );
1914 $ response = array (
2015 "input " => array ("size " => 161885 , "type " => "image/png " ),
@@ -29,6 +24,7 @@ function mock_jpg_response() {
2924 $ session ['Compression-Count ' ] += 1 ;
3025 header ('HTTP/1.1 201 Created ' );
3126 header ("Location: http://webservice/output/example.jpg " );
27+ header ("Content-Type: application/json; charset=utf-8 " );
3228 header ("Compression-Count: {$ session ['Compression-Count ' ]}" );
3329
3430 $ response = array (
@@ -44,6 +40,7 @@ function mock_large_response() {
4440 $ session ['Compression-Count ' ] += 1 ;
4541 header ('HTTP/1.1 201 Created ' );
4642 header ("Location: http://webservice/output/large.png " );
43+ header ("Content-Type: application/json; charset=utf-8 " );
4744 header ("Compression-Count: {$ session ['Compression-Count ' ]}" );
4845
4946 $ response = array (
@@ -53,19 +50,6 @@ function mock_large_response() {
5350 return json_encode ($ response );
5451}
5552
56- function mock_invalid_response () {
57- global $ session ;
58-
59- header ('HTTP/1.1 401 Unauthorized ' );
60- header ("Content-Type: application/json; charset=utf-8 " );
61-
62- $ response = array (
63- "error " => "Unauthorized " ,
64- "message " => "Credentials are invalid "
65- );
66- return json_encode ($ response );
67- }
68-
6953function mock_empty_response () {
7054 global $ session ;
7155
@@ -100,31 +84,12 @@ function mock_invalid_json_response() {
10084 $ session ['Compression-Count ' ] += 1 ;
10185 header ('HTTP/1.1 201 Created ' );
10286 header ("Location: http://webservice/output/example.png " );
87+ header ("Content-Type: application/json; charset=utf-8 " );
10388 header ("Compression-Count: {$ session ['Compression-Count ' ]}" );
10489 return '{invalid: json} ' ;
10590}
10691
107- function mock_resized_response () {
108- global $ session ;
109-
110- $ session ['Compression-Count ' ] += 1 ;
111- header ('HTTP/1.1 201 Created ' );
112- header ("Location: http://webservice/output/resized.jpg " );
113- header ("Compression-Count: {$ session ['Compression-Count ' ]}" );
114- $ response = array (
115- "input " => array ("size " => 161885 , "type " => "image/jpg " ),
116- "output " => array ("size " => 14238 , "type " => "image/jpg " , "ratio " => 0.95 , "width " => 1080 , "height " => 720 )
117- );
118- return json_encode ($ response );
119- }
120-
121- $ request_headers = apache_request_headers ();
122- $ basic_auth = base64_decode (str_replace ('Basic ' , '' , $ request_headers ['Authorization ' ]));
123- $ api_key_elements = explode (': ' , $ basic_auth );
124- $ api_key = $ api_key_elements [1 ];
125-
126- header ("Content-Type: application/json; charset=utf-8 " );
127-
92+ $ api_key = get_api_key ();
12893if ($ api_key == 'PNG123 ' ) {
12994 if (intval ($ _SERVER ['CONTENT_LENGTH ' ]) == 0 ) {
13095 echo mock_empty_response ();
@@ -145,16 +110,8 @@ function mock_resized_response() {
145110 }
146111} else if ($ api_key == 'LIMIT123 ' ) {
147112 echo mock_limit_reached_response ();
148- } else if ($ api_key == 'RESIZE123 ' ) {
149- if (intval ($ _SERVER ['CONTENT_LENGTH ' ]) == 0 ) {
150- echo mock_empty_response ();
151- } else {
152- echo mock_resized_response ();
153- }
154113} else {
155114 echo mock_invalid_response ();
156115}
157116
158- file_put_contents (SESSION_FILE , serialize ($ session ));
159-
160- ob_end_flush ();
117+ ob_end_flush ();
0 commit comments