blob: 5f2c8914c2574959a6af46efe696aa5a56e2df6e [file] [log] [blame]
Davide Pesavento56a741f2018-02-10 16:30:59 -05001#!/usr/bin/env python3
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07002# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
3
4"""
Davide Pesavento56a741f2018-02-10 16:30:59 -05005Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shie2733332016-11-24 14:11:40 +00006 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070012
13This file is part of NFD (Named Data Networking Forwarding Daemon).
14See AUTHORS.md for complete list of NFD authors and contributors.
15
16NFD is free software: you can redistribute it and/or modify it under the terms
17of the GNU General Public License as published by the Free Software Foundation,
18either version 3 of the License, or (at your option) any later version.
19
20NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
21without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22PURPOSE. See the GNU General Public License for more details.
23
24You should have received a copy of the GNU General Public License along with
25NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
26"""
Chengyu Fanb07788a2014-03-31 12:15:36 -060027
Davide Pesavento56a741f2018-02-10 16:30:59 -050028from http.server import HTTPServer, SimpleHTTPRequestHandler
29from socketserver import ThreadingMixIn
30import argparse, ipaddress, os, socket, subprocess
Chengyu Fanb07788a2014-03-31 12:15:36 -060031
32
Davide Pesavento56a741f2018-02-10 16:30:59 -050033class NfdStatusHandler(SimpleHTTPRequestHandler):
34 """ The handler class to handle requests """
Chengyu Fanb07788a2014-03-31 12:15:36 -060035 def do_GET(self):
Davide Pesavento56a741f2018-02-10 16:30:59 -050036 if self.path == "/":
37 self.__serveReport()
38 elif self.path == "/robots.txt" and self.server.allowRobots:
39 self.send_error(404)
Chengyu Fanb07788a2014-03-31 12:15:36 -060040 else:
Davide Pesavento56a741f2018-02-10 16:30:59 -050041 super().do_GET()
Chengyu Fanb07788a2014-03-31 12:15:36 -060042
Davide Pesavento56a741f2018-02-10 16:30:59 -050043 def __serveReport(self):
44 """ Obtain XML-formatted NFD status report and send it back as response body """
Junxiao Shi9137e9e2016-12-15 21:31:50 +000045 try:
Davide Pesavento56a741f2018-02-10 16:30:59 -050046 # enable universal_newlines to get output as string rather than byte sequence
47 output = subprocess.check_output(["nfdc", "status", "report", "xml"], universal_newlines=True)
48 except OSError as err:
49 super().log_message("error invoking nfdc: {}".format(err))
50 self.send_error(500)
51 except subprocess.CalledProcessError as err:
52 super().log_message("error invoking nfdc: command exited with status {}".format(err.returncode))
53 self.send_error(504, "Cannot connect to NFD (code {})".format(err.returncode))
54 else:
Junxiao Shi9137e9e2016-12-15 21:31:50 +000055 # add stylesheet processing instruction after the XML document type declaration
Davide Pesavento56a741f2018-02-10 16:30:59 -050056 # (yes, this is a ugly hack)
57 pos = output.index(">") + 1
Junxiao Shi9137e9e2016-12-15 21:31:50 +000058 xml = output[:pos]\
59 + '<?xml-stylesheet type="text/xsl" href="nfd-status.xsl"?>'\
60 + output[pos:]
Davide Pesavento56a741f2018-02-10 16:30:59 -050061 self.send_response(200)
62 self.send_header("Content-Type", "text/xml; charset=UTF-8")
63 self.end_headers()
64 self.wfile.write(xml.encode())
Chengyu Fanb07788a2014-03-31 12:15:36 -060065
Davide Pesavento56a741f2018-02-10 16:30:59 -050066 # override
67 def log_message(self, *args):
68 if self.server.verbose:
69 super().log_message(*args)
70
71
72class ThreadingHttpServer(ThreadingMixIn, HTTPServer):
Chengyu Fane25b0f02014-04-05 21:42:40 -060073 """ Handle requests using threads """
Davide Pesavento56a741f2018-02-10 16:30:59 -050074 def __init__(self, bindAddr, port, handler, allowRobots=False, verbose=False):
75 # socketserver.BaseServer defaults to AF_INET even if you provide an IPv6 address
76 # see https://bugs.python.org/issue20215 and https://bugs.python.org/issue24209
77 if bindAddr.version == 6:
Chengyu Fane25b0f02014-04-05 21:42:40 -060078 self.address_family = socket.AF_INET6
Davide Pesavento56a741f2018-02-10 16:30:59 -050079 self.allowRobots = allowRobots
Chengyu Fanb07788a2014-03-31 12:15:36 -060080 self.verbose = verbose
Davide Pesavento56a741f2018-02-10 16:30:59 -050081 super().__init__((str(bindAddr), port), handler)
Chengyu Fanb07788a2014-03-31 12:15:36 -060082
Davide Pesavento56a741f2018-02-10 16:30:59 -050083
84def main():
85 def ipAddress(arg):
86 """ Validate IP address """
Chengyu Fane25b0f02014-04-05 21:42:40 -060087 try:
Davide Pesavento56a741f2018-02-10 16:30:59 -050088 value = ipaddress.ip_address(arg)
89 except ValueError:
90 raise argparse.ArgumentTypeError("{!r} is not a valid IP address".format(arg))
91 return value
92
93 def portNumber(arg):
94 """ Validate port number """
Chengyu Fane25b0f02014-04-05 21:42:40 -060095 try:
Davide Pesavento56a741f2018-02-10 16:30:59 -050096 value = int(arg)
97 except ValueError:
98 value = -1
99 if value < 0 or value > 65535:
100 raise argparse.ArgumentTypeError("{!r} is not a valid port number".format(arg))
101 return value
Chengyu Fane25b0f02014-04-05 21:42:40 -0600102
Davide Pesavento56a741f2018-02-10 16:30:59 -0500103 parser = argparse.ArgumentParser(description="Serves NFD status page via HTTP")
104 parser.add_argument("-V", "--version", action="version", version="@VERSION@")
105 parser.add_argument("-a", "--address", default="127.0.0.1", type=ipAddress, metavar="ADDR",
106 help="bind to this IP address (default: %(default)s)")
107 parser.add_argument("-p", "--port", default=8080, type=portNumber,
108 help="bind to this port number (default: %(default)s)")
109 parser.add_argument("-f", "--workdir", default="@DATAROOTDIR@/ndn", metavar="DIR",
110 help="server's working directory (default: %(default)s)")
111 parser.add_argument("-r", "--robots", action="store_true",
112 help="allow crawlers and other HTTP bots")
113 parser.add_argument("-v", "--verbose", action="store_true",
114 help="turn on verbose logging")
115 args = parser.parse_args()
Chengyu Fanb07788a2014-03-31 12:15:36 -0600116
Davide Pesavento56a741f2018-02-10 16:30:59 -0500117 os.chdir(args.workdir)
Chengyu Fanb07788a2014-03-31 12:15:36 -0600118
Davide Pesavento56a741f2018-02-10 16:30:59 -0500119 httpd = ThreadingHttpServer(args.address, args.port, NfdStatusHandler,
120 allowRobots=args.robots, verbose=args.verbose)
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700121
Chengyu Fane25b0f02014-04-05 21:42:40 -0600122 if httpd.address_family == socket.AF_INET6:
Davide Pesavento56a741f2018-02-10 16:30:59 -0500123 url = "http://[{}]:{}"
Chengyu Fane25b0f02014-04-05 21:42:40 -0600124 else:
Davide Pesavento56a741f2018-02-10 16:30:59 -0500125 url = "http://{}:{}"
126 print("Server started at", url.format(*httpd.server_address))
Chengyu Fanb07788a2014-03-31 12:15:36 -0600127
128 try:
129 httpd.serve_forever()
130 except KeyboardInterrupt:
131 pass
Chengyu Fanb07788a2014-03-31 12:15:36 -0600132 httpd.server_close()
133
Chengyu Fanb07788a2014-03-31 12:15:36 -0600134
Davide Pesavento56a741f2018-02-10 16:30:59 -0500135if __name__ == "__main__":
136 main()