diff options
Diffstat (limited to 'queue.php')
-rw-r--r-- | queue.php | 173 |
1 files changed, 129 insertions, 44 deletions
@@ -1,6 +1,7 @@ <?php include_once "db.php"; include_once "UrlGenerator.class.php"; +include_once "Templates.class.php"; header("Content-Type: text/plain"); @@ -12,9 +13,10 @@ function data_file($id) { return "/tmp/mq/data/$id"; } -/* -var_dump($_SERVER); -*/ +function log_file($id) { + return "/tmp/mq/data/" . $id . ".log"; +} + $method = $_SERVER["REQUEST_METHOD"]; $content_type = isset($_SERVER["CONTENT_TYPE"]) ? $_SERVER["CONTENT_TYPE"] : NULL; $db = new DB(); @@ -29,7 +31,7 @@ if(isset($content_type) && $content_type == "application/x-www-form-urlencoded") list($key, $value) = split("=", $part); $form[$key] = $value; } - var_dump($form); +// var_dump($form); } $path_info = $_SERVER["PATH_INFO"]; @@ -41,7 +43,10 @@ else { $path_info = preg_replace('/[\/]$/', '', $path_info); $path_info = preg_replace('/^[\/]/', '', $path_info); $segments = explode("/", $path_info); - $baseurl = "http://" . $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["SCRIPT_NAME"]; +// var_dump($segments); + $port = $_SERVER["SERVER_PORT"]; + $port = $port == "80" ? "" : ":" . $port; + $baseurl = "http://" . $_SERVER["SERVER_NAME"] . $port . $_SERVER["SCRIPT_NAME"]; $url_generator = new UrlGenerator($baseurl); switch($segments[0]) { case "enqueue": @@ -51,20 +56,23 @@ else { } enqueue($url_generator, $segments[1]); break; - case "item": - allow_methods("GET"); - if(count($segments) != 3) { - not_found(); - } - item($url_generator, $segments[1], $segments[2]); - break; case "queue": switch(count($segments)) { case 1: - queue($url_generator); + queues($url_generator); break; case 2: - queues($url_generator, $segments[1]); + queue($url_generator, $segments[1]); + break; + case 3: + if($segments[2] == "items") { + items($url_generator, $segments[1]); + } + break; + case 4: + if($segments[2] == "items") { + item($url_generator, $segments[1], $segments[3]); + } break; default: not_found(); @@ -126,6 +134,17 @@ function bad_request($short = NULL, $long = NULL) { exit(0); } +function conneg() { + $accept = isset($_SERVER["HTTP_ACCEPT"]) ? $_SERVER["HTTP_ACCEPT"] : NULL; + // First media type + $accept = preg_replace('/^([^,]*),.*/', '$1', $accept); +// var_dump($accept); + // Remove parameters + $accept = preg_replace('/^([^;]*);.*/', '$1', $accept); +// var_dump($accept); + return $accept; +} + function cron() { global $db; @@ -152,6 +171,18 @@ function cron() { function process_item($queue, $item) { echo("Processing item " . $item->id . " from queue " . $queue->name . "\n"); + $input_path = data_file($item->id); + $input_fd = fopen($input_path, "r"); + + $log_path = log_file($item->id); + $log_fd = fopen($log_path, "w"); + + if(!$input_fd || !$log_fd) { + echo("Error opening " . $path . "\n"); + fclose($input_fd); + fclose($log_fd); + return; + } $handle = curl_init(); // http://no2.php.net/manual/en/function.curl-setopt.php @@ -170,26 +201,34 @@ function process_item($queue, $item) { curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 1); // Return the transfer as a string of the return value of curl_exec() - curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); +// curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // In seconds curl_setopt($handle, CURLOPT_TIMEOUT, 30); curl_setopt($handle, CURLOPT_URL, $queue->endpoint_url); - var_dump($item->headers); - curl_setopt($handle, CURLOPT_HEADER, $item->headers); + curl_setopt($handle, CURLOPT_HTTPHEADER, $item->headers); + curl_setopt($handle, CURLOPT_INFILE, $input_fd); + + curl_setopt($handle, CURLOPT_WRITEHEADER, $log_fd); + curl_setopt($handle, CURLOPT_FILE, $log_fd); + curl_setopt($handle, CURLOPT_STDERR, $log_fd); - curl_setopt($handle, CURLOPT_INFILE, data_file($item->id)); + curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); + echo("Executing request...\n"); + fwrite($log_fd, "POST " . $queue->endpoint_url . "\n"); $ret = curl_exec($handle); + if($ret === false) { - echo("curl failed"); - echo("error: " . curl_error($handle)); + echo("curl failed\n"); + echo("error: " . curl_error($handle) . "\n"); } else { - echo("curl success"); - echo($ret); + echo("curl success\n"); } + fclose($input_fd); + fclose($log_fd); curl_close($handle); } @@ -219,25 +258,62 @@ function enqueue($url_generator, $queue) { $db->insert_item($id, $headers, array($queue)); header("HTTP/1.1 201 Created as " . $id); - header("Location: " . $url_generator->item($id)); + header("Location: " . $url_generator->item($queue, $id)); } catch (Exception $e) { header("HTTP/1.1 500 Error"); echo("Caught exception: " . $e->getMessage() . "\n"); } } -function item($url_generator, $queue, $id) { - global $db; +function item($url_generator, $queue, $item) { + global $db, $method; + + list($x, $queue, $id) = $segments; $item = $db->select_item($queue, $id); -// var_dump($item); - $item = $item[0]; - header("Link: <" . $url_generator->item_enclosure($item->id) . ">;rel=enclosure"); - echo("Showing: " . $item->id . "\n"); - echo($item->toPlainText($url_generator)); + if($item == NULL) { + not_found("No such item: " . $id); + } + + switch($method) { + case "GET": + header("Link: <" . $url_generator->item_enclosure($item->id) . ">;rel=enclosure"); + echo("Showing: " . $item->id . "\n"); + echo($item->toPlainText($url_generator)); + break; + case "DELETE": + $db->delete_item($queue, $id); + @unlink(data_file($id)); + @unlink(log_file($id)); + header("HTTP/1.1 204 Deleted item " . $id); + break; + default: + allow_methods("GET", "DELETE"); + break; + } +} + +function items($url_generator, $queue) { + global $db; + + allow_methods("GET"); + + header("HTTP/1.1 200 Items in $queue"); + $items = $db->select_items($queue); + switch(conneg()) { + default: + case "text/plain": + header("Content-Type: text/plain"); + template_item_list_text($url_generator, $queue, $items); + break; + case "text/html": + header("Content-Type: text/html"); + template_item_list_html($url_generator, $queue, $items); + break; + } } -function queue($url_generator) { +function queues($url_generator) { global $method, $content_type, $db; switch($method) { @@ -246,16 +322,16 @@ function queue($url_generator) { $queues = $db->select_queues(); $count = count($queues); - header("Content-Type: text/plain"); - if($count == 1) { - echo("Have one queue"); - } - else { - echo("Have " . $count . " queues"); - } - echo("\n\n"); - foreach($queues as $q) { - echo($q->toPlainText($url_generator) . "\n"); + switch(conneg()) { + default: + case "text/plain": + header("Content-Type: text/plain"); + template_queue_list_text($url_generator, $queues); + break; + case "text/html": + header("Content-Type: text/html"); + template_queue_list_html($url_generator, $queues); + break; } break; case "POST": @@ -278,7 +354,7 @@ function queue($url_generator) { } } -function queues($url_generator, $queue) { +function queue($url_generator, $queue) { global $method, $content_type, $form, $db; if(!Queue::valid_name($queue)) { @@ -293,8 +369,17 @@ function queues($url_generator, $queue) { not_found("No such queue: " . $queue); } - header("Content-Type: text/plain"); - echo($q->toPlainText($url_generator)); + switch(conneg()) { + case "text/html"; + header("Content-Type: text/html"); + template_queue_html($url_generator, $q); + break; + default: + case "text/plain"; + header("Content-Type: text/plain"); + template_queue_text($url_generator, $q); + break; + } break; case "PUT": $q = $db->select_queue($queue); |