Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
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 | """ |
| 5 | Copyright (c) 2014 Regents of the University of California, |
| 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 | |
| 12 | This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 13 | See AUTHORS.md for complete list of NFD authors and contributors. |
| 14 | |
| 15 | NFD is free software: you can redistribute it and/or modify it under the terms |
| 16 | of the GNU General Public License as published by the Free Software Foundation, |
| 17 | either version 3 of the License, or (at your option) any later version. |
| 18 | |
| 19 | NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 20 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 21 | PURPOSE. See the GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License along with |
| 24 | NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 25 | """ |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 26 | |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 27 | from BaseHTTPServer import HTTPServer |
| 28 | from SimpleHTTPServer import SimpleHTTPRequestHandler |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 29 | from SocketServer import ThreadingMixIn |
| 30 | import sys |
Chengyu Fan | 30aa207 | 2014-07-20 13:52:32 -0600 | [diff] [blame] | 31 | import subprocess |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 32 | import StringIO |
| 33 | import urlparse |
| 34 | import logging |
| 35 | import cgi |
| 36 | import argparse |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 37 | import socket |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 38 | import os |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 39 | |
| 40 | |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 41 | class StatusHandler(SimpleHTTPRequestHandler): |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 42 | """ The handler class to handle requests.""" |
| 43 | def do_GET(self): |
| 44 | # get the url info to decide how to respond |
| 45 | parsedPath = urlparse.urlparse(self.path) |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 46 | if parsedPath.path == "/": |
| 47 | # get current nfd status, and use it as result message |
| 48 | (res, resultMessage) = self.getNfdStatus() |
| 49 | self.send_response(200) |
| 50 | if res == 0: |
| 51 | self.send_header("Content-type", "text/xml; charset=UTF-8") |
| 52 | else: |
| 53 | self.send_header("Content-type", "text/html; charset=UTF-8") |
| 54 | |
| 55 | self.end_headers() |
| 56 | self.wfile.write(resultMessage) |
| 57 | elif parsedPath.path == "/robots.txt" and self.server.robots == True: |
| 58 | resultMessage = "" |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 59 | self.send_response(200) |
| 60 | self.send_header("Content-type", "text/plain") |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 61 | self.end_headers() |
| 62 | self.wfile.write(resultMessage) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 63 | else: |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 64 | SimpleHTTPRequestHandler.do_GET(self) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 65 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 66 | def log_message(self, format, *args): |
| 67 | if self.server.verbose: |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 68 | logging.info("%s - %s\n" % (self.address_string(), |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 69 | format % args)) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 70 | |
| 71 | def getNfdStatus(self): |
| 72 | """ |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 73 | This function is to call nfd-status command |
| 74 | to get xml format output |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 75 | """ |
Chengyu Fan | 30aa207 | 2014-07-20 13:52:32 -0600 | [diff] [blame] | 76 | sp = subprocess.Popen(['nfd-status', '-x'], stdout=subprocess.PIPE) |
| 77 | res = sp.wait() |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 78 | if res == 0: |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 79 | # add the xml-stylesheet processing instruction after the 1st '>' symbol |
Chengyu Fan | 30aa207 | 2014-07-20 13:52:32 -0600 | [diff] [blame] | 80 | output = sp.stdout.read() |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 81 | newLineIndex = output.index('>') + 1 |
| 82 | resultStr = output[:newLineIndex]\ |
| 83 | + "<?xml-stylesheet type=\"text/xsl\" href=\"nfd-status.xsl\"?>"\ |
| 84 | + output[newLineIndex:] |
| 85 | return (res, resultStr) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 86 | else: |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 87 | htmlStr = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional'\ |
| 88 | + '//EN" "http://www.w3.org/TR/html4/loose.dtd">\n'\ |
| 89 | + '<html><head><title>NFD status</title>'\ |
| 90 | + '<meta http-equiv="Content-type" content="text/html;'\ |
| 91 | + 'charset=UTF-8"></head>\n<body>' |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 92 | # return connection error code |
| 93 | htmlStr = htmlStr + "<p>Cannot connect to NFD,"\ |
| 94 | + " Code = " + str(res) + "</p>\n" |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 95 | htmlStr = htmlStr + "</body></html>" |
| 96 | return (res, htmlStr) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 97 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 98 | class ThreadHttpServer(ThreadingMixIn, HTTPServer): |
| 99 | """ Handle requests using threads """ |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 100 | def __init__(self, server, handler, verbose=False, robots=False): |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 101 | serverAddr = server[0] |
| 102 | # socket.AF_UNSPEC is not supported, check whether it is v6 or v4 |
| 103 | ipType = self.getIpType(serverAddr) |
| 104 | if ipType == socket.AF_INET6: |
| 105 | self.address_family = socket.AF_INET6 |
| 106 | elif ipType == socket.AF_INET: |
| 107 | self.address_family == socket.AF_INET |
| 108 | else: |
| 109 | logging.error("The input IP address is neither IPv6 nor IPv4") |
| 110 | sys.exit(2) |
| 111 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 112 | try: |
| 113 | HTTPServer.__init__(self, server, handler) |
| 114 | except Exception as e: |
| 115 | logging.error(str(e)) |
| 116 | sys.exit(2) |
| 117 | self.verbose = verbose |
| 118 | self.robots = robots |
| 119 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 120 | def getIpType(self, ipAddr): |
| 121 | """ Get ipAddr's address type """ |
| 122 | # if ipAddr is an IPv6 addr, return AF_INET6 |
| 123 | try: |
| 124 | socket.inet_pton(socket.AF_INET6, ipAddr) |
| 125 | return socket.AF_INET6 |
| 126 | except socket.error: |
| 127 | pass |
| 128 | # if ipAddr is an IPv4 addr return AF_INET, if not, return None |
| 129 | try: |
| 130 | socket.inet_pton(socket.AF_INET, ipAddr) |
| 131 | return socket.AF_INET |
| 132 | except socket.error: |
| 133 | return None |
| 134 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 135 | |
| 136 | # main function to start |
| 137 | def httpServer(): |
| 138 | parser = argparse.ArgumentParser() |
| 139 | parser.add_argument("-p", type=int, metavar="port number", |
| 140 | help="Specify the HTTP server port number, default is 8080.", |
| 141 | dest="port", default=8080) |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 142 | # if address is not specified, use 127.0.0.1 |
| 143 | parser.add_argument("-a", default="127.0.0.1", metavar="IP address", dest="addr", |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 144 | help="Specify the HTTP server IP address.") |
| 145 | parser.add_argument("-r", default=False, dest="robots", action="store_true", |
| 146 | help="Enable HTTP robots to crawl; disabled by default.") |
Alexander Afanasyev | 8a09376 | 2014-07-16 18:43:09 -0700 | [diff] [blame] | 147 | parser.add_argument("-f", default="@DATAROOTDIR@/ndn", metavar="Server Directory", dest="serverDir", |
| 148 | help="Specify the working directory of nfd-status-http-server, default is @DATAROOTDIR@/ndn.") |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 149 | parser.add_argument("-v", default=False, dest="verbose", action="store_true", |
| 150 | help="Verbose mode.") |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 151 | parser.add_argument("--version", default=False, dest="version", action="store_true", |
| 152 | help="Show version and exit") |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 153 | |
| 154 | args = vars(parser.parse_args()) |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 155 | |
| 156 | if args['version']: |
| 157 | print "@VERSION@" |
| 158 | return |
| 159 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 160 | localPort = args["port"] |
| 161 | localAddr = args["addr"] |
| 162 | verbose = args["verbose"] |
| 163 | robots = args["robots"] |
Chengyu Fan | 45d1a76 | 2014-07-08 14:21:32 -0600 | [diff] [blame] | 164 | serverDirectory = args["serverDir"] |
| 165 | |
| 166 | os.chdir(serverDirectory) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 167 | |
| 168 | # setting log message format |
| 169 | logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', |
| 170 | level=logging.INFO) |
| 171 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 172 | # if port is invalid, exit |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 173 | if localPort <= 0 or localPort > 65535: |
| 174 | logging.error("Specified port number is invalid") |
| 175 | sys.exit(2) |
| 176 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 177 | httpd = ThreadHttpServer((localAddr, localPort), StatusHandler, |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 178 | verbose, robots) |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 179 | httpServerAddr = "" |
| 180 | if httpd.address_family == socket.AF_INET6: |
| 181 | httpServerAddr = "http://[%s]:%s" % (httpd.server_address[0], |
| 182 | httpd.server_address[1]) |
| 183 | else: |
| 184 | httpServerAddr = "http://%s:%s" % (httpd.server_address[0], |
| 185 | httpd.server_address[1]) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 186 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 187 | logging.info("Server started - at %s" % httpServerAddr) |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 188 | |
| 189 | try: |
| 190 | httpd.serve_forever() |
| 191 | except KeyboardInterrupt: |
| 192 | pass |
| 193 | |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 194 | httpd.server_close() |
| 195 | |
Chengyu Fan | e25b0f0 | 2014-04-05 21:42:40 -0600 | [diff] [blame] | 196 | logging.info("Server stopped") |
Chengyu Fan | b07788a | 2014-03-31 12:15:36 -0600 | [diff] [blame] | 197 | |
| 198 | |
| 199 | if __name__ == '__main__': |
| 200 | httpServer() |