Example #1
0
 /**
  * Handle a GET request for this resource
  * @param Request request
  * @return Response
  */
 function get($request)
 {
     $response = new Response($request);
     $etag = md5($request->uri);
     if ($request->ifNoneMatch($etag)) {
         $response->code = Response::NOTMODIFIED;
     } else {
         $response->code = Response::OK;
         $response->addHeader('Content-type', 'text/plain');
         $response->addEtag($etag);
         $response->body = "Hello world!\n" . "\n" . "This request:\n" . "\n" . $request->__toString() . "\n" . "\n" . "This response:\n" . "\n" . $response->__toString();
     }
     return $response;
 }
Example #2
0
 /**
  * Handle a GET request for this resource by returning the contents of a file matching the request URI
  * @param Request request
  * @return Response
  */
 function get($request)
 {
     // look at all candidate URIs in turn and stop when we find a file that matches one
     foreach ($request->negotiatedUris as $uri) {
         // convert URI into filesystem path
         $filePath = $this->turnUriIntoFilePath($uri, $request);
         if (substr($filePath, -1, 1) == DIRECTORY_SEPARATOR) {
             // add a default filename to the path
             $filePath .= $this->defaultDocument;
             $uri .= $this->defaultDocument;
         }
         if (file_exists($filePath)) {
             // use this file
             $response = new Response($request, $uri);
             // generate etag for the resource based on the files modified date
             $etag = md5(filemtime($filePath));
             if ($request->ifNoneMatch($etag)) {
                 // client has matching etag
                 $response->code = Response::NOTMODIFIED;
             } else {
                 $explodedPath = explode('.', $filePath);
                 $extension = array_pop($explodedPath);
                 if (isset($request->mimetypes[$extension])) {
                     // add content type header
                     $response->addHeader('Content-Type', $request->mimetypes[$extension]);
                 }
                 $response->addEtag($etag);
                 // add etag header
                 $response->body = file_get_contents($filePath);
                 // set contents
             }
             return $response;
         }
     }
     // nothing found, send 404 response
     $response = new Response($request);
     $response->code = Response::NOTFOUND;
     $response->addHeader('Content-Type', $request->mimetypes['html']);
     $response->body = '<p>404, nothing found</p>';
     return $response;
 }