Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 2 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 3 | |
| 4 | """ |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 5 | Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | e273333 | 2016-11-24 14:11:40 +0000 | [diff] [blame] | 6 | Arizona Board of Regents, |
| 7 | Colorado State University, |
| 8 | University Pierre & Marie Curie, Sorbonne University, |
| 9 | Washington University in St. Louis, |
| 10 | Beijing Institute of Technology, |
| 11 | The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 12 | |
| 13 | This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 14 | See AUTHORS.md for complete list of NFD authors and contributors. |
| 15 | |
| 16 | NFD is free software: you can redistribute it and/or modify it under the terms |
| 17 | of the GNU General Public License as published by the Free Software Foundation, |
| 18 | either version 3 of the License, or (at your option) any later version. |
| 19 | |
| 20 | NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 21 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 22 | PURPOSE. See the GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License along with |
| 25 | NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 26 | """ |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 27 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 28 | from http.server import HTTPServer, SimpleHTTPRequestHandler |
| 29 | from socketserver import ThreadingMixIn |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 30 | from urllib.parse import urlsplit |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 31 | import argparse, ipaddress, os, socket, subprocess |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 32 | |
| 33 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 34 | class NfdStatusHandler(SimpleHTTPRequestHandler): |
| 35 | """ The handler class to handle requests """ |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 36 | def do_GET(self): |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 37 | path = urlsplit(self.path).path |
| 38 | if path == "/": |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 39 | self.__serveReport() |
Davide Pesavento | b922c88 | 2018-02-24 23:55:57 -0500 | [diff] [blame] | 40 | elif path == "/robots.txt" and self.server.allowRobots: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 41 | self.send_error(404) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 42 | else: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 43 | super().do_GET() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 44 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 45 | def __serveReport(self): |
| 46 | """ Obtain XML-formatted NFD status report and send it back as response body """ |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 47 | try: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 48 | # enable universal_newlines to get output as string rather than byte sequence |
| 49 | output = subprocess.check_output(["nfdc", "status", "report", "xml"], universal_newlines=True) |
| 50 | except OSError as err: |
| 51 | super().log_message("error invoking nfdc: {}".format(err)) |
| 52 | self.send_error(500) |
| 53 | except subprocess.CalledProcessError as err: |
| 54 | super().log_message("error invoking nfdc: command exited with status {}".format(err.returncode)) |
| 55 | self.send_error(504, "Cannot connect to NFD (code {})".format(err.returncode)) |
| 56 | else: |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 57 | # add stylesheet processing instruction after the XML document type declaration |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 58 | # (yes, this is a ugly hack) |
| 59 | pos = output.index(">") + 1 |
Junxiao Shi | 9137e9e | 2016-12-15 21:31:50 +0000 | [diff] [blame] | 60 | xml = output[:pos]\ |
| 61 | + '<?xml-stylesheet type="text/xsl" href="nfd-status.xsl"?>'\ |
| 62 | + output[pos:] |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 63 | self.send_response(200) |
| 64 | self.send_header("Content-Type", "text/xml; charset=UTF-8") |
| 65 | self.end_headers() |
| 66 | self.wfile.write(xml.encode()) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 67 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 68 | # override |
| 69 | def log_message(self, *args): |
| 70 | if self.server.verbose: |
| 71 | super().log_message(*args) |
| 72 | |
| 73 | |
| 74 | class ThreadingHttpServer(ThreadingMixIn, HTTPServer): |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 75 | """ Handle requests using threads """ |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 76 | def __init__(self, bindAddr, port, handler, allowRobots=False, verbose=False): |
| 77 | # socketserver.BaseServer defaults to AF_INET even if you provide an IPv6 address |
| 78 | # see https://bugs.python.org/issue20215 and https://bugs.python.org/issue24209 |
| 79 | if bindAddr.version == 6: |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 80 | self.address_family = socket.AF_INET6 |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 81 | self.allowRobots = allowRobots |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 82 | self.verbose = verbose |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 83 | super().__init__((str(bindAddr), port), handler) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 84 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 85 | |
| 86 | def main(): |
| 87 | def ipAddress(arg): |
| 88 | """ Validate IP address """ |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 89 | try: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 90 | value = ipaddress.ip_address(arg) |
| 91 | except ValueError: |
| 92 | raise argparse.ArgumentTypeError("{!r} is not a valid IP address".format(arg)) |
| 93 | return value |
| 94 | |
| 95 | def portNumber(arg): |
| 96 | """ Validate port number """ |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 97 | try: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 98 | value = int(arg) |
| 99 | except ValueError: |
| 100 | value = -1 |
| 101 | if value < 0 or value > 65535: |
| 102 | raise argparse.ArgumentTypeError("{!r} is not a valid port number".format(arg)) |
| 103 | return value |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 104 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 105 | parser = argparse.ArgumentParser(description="Serves NFD status page via HTTP") |
| 106 | parser.add_argument("-V", "--version", action="version", version="@VERSION@") |
| 107 | parser.add_argument("-a", "--address", default="127.0.0.1", type=ipAddress, metavar="ADDR", |
| 108 | help="bind to this IP address (default: %(default)s)") |
| 109 | parser.add_argument("-p", "--port", default=8080, type=portNumber, |
| 110 | help="bind to this port number (default: %(default)s)") |
| 111 | parser.add_argument("-f", "--workdir", default="@DATAROOTDIR@/ndn", metavar="DIR", |
| 112 | help="server's working directory (default: %(default)s)") |
| 113 | parser.add_argument("-r", "--robots", action="store_true", |
| 114 | help="allow crawlers and other HTTP bots") |
| 115 | parser.add_argument("-v", "--verbose", action="store_true", |
| 116 | help="turn on verbose logging") |
| 117 | args = parser.parse_args() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 118 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 119 | os.chdir(args.workdir) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 120 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 121 | httpd = ThreadingHttpServer(args.address, args.port, NfdStatusHandler, |
| 122 | allowRobots=args.robots, verbose=args.verbose) |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 123 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 124 | if httpd.address_family == socket.AF_INET6: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 125 | url = "http://[{}]:{}" |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 126 | else: |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 127 | url = "http://{}:{}" |
| 128 | print("Server started at", url.format(*httpd.server_address)) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 129 | |
| 130 | try: |
| 131 | httpd.serve_forever() |
| 132 | except KeyboardInterrupt: |
| 133 | pass |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 134 | httpd.server_close() |
| 135 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 136 | |
Davide Pesavento | 56a741f | 2018-02-10 16:30:59 -0500 | [diff] [blame] | 137 | if __name__ == "__main__": |
| 138 | main() |