1#!@PYTHON@
2
3# Copyright (C) 2016 Jakub Kruszona-Zawadzki, Core Technology Sp. z o.o.
4#
5# This file is part of MooseFS.
6#
7# MooseFS is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, version 2 (only).
10#
11# MooseFS is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with MooseFS; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
19# or visit http://www.gnu.org/licenses/gpl-2.0.html
20
21import socket
22import struct
23import cgi
24import cgitb; cgitb.enable()
25import sys
26
27PROTO_BASE = @PROTO_BASE@
28
29CUTOAN_CHART = (PROTO_BASE+504)
30ANTOCU_CHART = (PROTO_BASE+505)
31
32fields = cgi.FieldStorage()
33
34if "host" in fields:
35	host = fields.getvalue("host")
36else:
37	host = ''
38if "port" in fields:
39	try:
40		port = int(fields.getvalue("port"))
41	except ValueError:
42		port = 0
43else:
44	port = 0
45if "id" in fields:
46	try:
47		chartid = int(fields.getvalue("id"))
48	except ValueError:
49		chartid = -1
50else:
51	chartid = -1
52if "width" in fields and "height" in fields:
53	try:
54		width = int(fields.getvalue("width"))
55		height = int(fields.getvalue("height"))
56	except ValueError:
57		width = 0
58		height = 0
59else:
60	width = 0
61	height = 0
62
63def mysend(socket,msg):
64	totalsent = 0
65	while totalsent < len(msg):
66		sent = socket.send(msg[totalsent:])
67		if sent == 0:
68			raise RuntimeError("socket connection broken")
69		totalsent = totalsent + sent
70
71def myrecv(socket,leng):
72	if sys.version<'3':
73		msg = ''
74	else:
75		msg = bytes(0)
76	while len(msg) < leng:
77		chunk = socket.recv(leng-len(msg))
78		if len(chunk) == 0:
79			raise RuntimeError("socket connection broken")
80		msg = msg + chunk
81	return msg
82
83def senderr():
84	print("Content-Type: image/gif")
85	print("")
86	sys.stdout.flush()
87	f = open('err.gif','rb')
88	if sys.version >= '3':
89		sys.stdout.buffer.write(f.read())
90	else:
91		sys.stdout.write(f.read())
92	f.close()
93
94if host=='' or port==0 or chartid<0:
95	senderr()
96else:
97	try:
98		s = socket.socket()
99		s.connect((host,port))
100		if width==0 and height==0:
101			mysend(s,struct.pack(">LLL",CUTOAN_CHART,4,chartid))
102		else:
103			mysend(s,struct.pack(">LLLHH",CUTOAN_CHART,8,chartid,width,height))
104		header = myrecv(s,8)
105		cmd,length = struct.unpack(">LL",header)
106		if cmd==ANTOCU_CHART and length>0:
107			data = myrecv(s,length)
108			if sys.version < '3':
109				if data[:3]=="GIF":
110					print("Content-Type: image/gif")
111					print("")
112					sys.stdout.flush()
113					sys.stdout.write(data)
114				elif data[:8]=="\x89PNG\x0d\x0a\x1a\x0a":
115					print("Content-Type: image/png")
116					print("")
117					sys.stdout.flush()
118					sys.stdout.write(data)
119				else:
120					senderr()
121			else:
122				if data[:3]=="GIF".encode("latin-1"):
123					print("Content-Type: image/gif")
124					print("")
125					sys.stdout.flush()
126					sys.stdout.buffer.write(data)
127				elif data[:8]=="\x89PNG\x0d\x0a\x1a\x0a".encode("latin-1"):
128					print("Content-Type: image/png")
129					print("")
130					sys.stdout.flush()
131					sys.stdout.buffer.write(data)
132				else:
133					senderr()
134		else:
135			senderr()
136		s.close()
137	except Exception:
138		senderr()
139