From e0a5561fda74908f19166844da6e08a6c02ff0e1 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 18 Mar 2012 11:52:54 +0000 Subject: o Adding html templates. --- queue.php | 173 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 129 insertions(+), 44 deletions(-) (limited to 'queue.php') diff --git a/queue.php b/queue.php index 26d0f30..2c7396f 100644 --- a/queue.php +++ b/queue.php @@ -1,6 +1,7 @@ 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); -- cgit v1.2.3