1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
|
#
# Collective Knowledge (CK web service)
#
# See CK LICENSE.txt for licensing details
# See CK COPYRIGHT.txt for copyright details
#
# Developer: Grigori Fursin
#
cfg={} # Will be updated by CK (meta description of this module)
work={} # Will be updated by CK (temporal data)
ck=None # Will be updated by CK (initialized CK kernel)
wfe_host=''
wfe_port=''
# Local settings
import os
import sys
import cgi
import urllib
import base64
import tempfile
# Import various modules while supporting both Python 2.x and 3.x
try:
from http.server import BaseHTTPRequestHandler, HTTPServer
except:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
try:
import urllib.parse as urlparse
except:
import urlparse
try:
from urllib.parse import quote as urlquote
except:
from urllib import quote as urlquote
try:
from urllib.parse import unquote as urlunquote
except:
from urllib import unquote as urlunquote
#try:
# import http.cookies as Cookie
#except:
# import Cookie
try:
from socketserver import ThreadingMixIn
except:
from SocketServer import ThreadingMixIn
##############################################################################
# Initialize module
def init(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
return {'return':0}
##############################################################################
# Access CK through CMD (can detach console)
def call_ck(i):
"""
Input: {
Input for CK
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
(stdout) - stdout, if available
(stderr) - stderr, if available
(std) - stdout+stderr
}
"""
import subprocess
import re
# Check action
action=i.get('action','')
if action=='':
return {'return':1, 'error':'action is not defined'}
# Check that no special characters, otherwise can run any command from CMD
if not re.match('^[A-Za-z0-9-_]*$', action):
return {'return':1, 'error':'action contains illegal characters'}
# Generate tmp file
fd, fn=tempfile.mkstemp(suffix='.tmp', prefix='ck-') # suffix is important - CK will delete such file!
os.close(fd)
dc=i.get('detach_console','')
if dc=='yes': i['out']='con' # If detach, output as console
# Prepare dummy output
rr={'return':0}
rr['stdout']=''
rr['stderr']=''
# Save json to temporay file
rx=ck.save_json_to_file({'json_file':fn, 'dict':i})
if rx['return']>0: return rx
# Prepare command line
cmd='ck '+action+' @'+fn
if dc=='yes':
# Check platform
rx=ck.get_os_ck({})
if rx['return']>0: return rx
plat=rx['platform']
dci=ck.cfg.get('detached_console',{}).get(plat,{})
dcmd=dci.get('cmd','')
if dcmd=='':
return {'return':1, 'error':'detached console is requested but cmd is not defined in kernel configuration'}
dcmd=dcmd.replace('$#cmd#$', cmd)
if dci.get('use_create_new_console_flag','')=='yes':
process=subprocess.Popen(dcmd, stdin=None, stdout=None, stderr=None, shell=True, close_fds=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
else:
# Will need to do the forking
try:
pid=os.fork()
except OSError as e:
return {'return':1, 'error':'forking detached console failed ('+format(e)+')'}
if pid==0:
os.setsid()
pid=os.fork()
if pid!=0: os._exit(0)
try:
maxfd=os.sysconf("SC_OPEN_MAX")
except (AttributeError, ValueError):
maxfd=1024
for fd in range(maxfd):
try:
os.close(fd)
except OSError:
pass
os.open('/dev/null', os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
# Normally child process
process=os.system(dcmd)
os._exit(0)
stdout=ck.cfg.get('detached_console_html', 'Console was detached ...')
stderr=''
else:
process=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout,stderr=process.communicate()
try: stdout=stdout.decode('utf8')
except Exception as e: pass
try: stderr=stderr.decode('utf8')
except Exception as e: pass
rr['std']=stdout+stderr
rr['stdout']=stdout
rr['stderr']=stderr
return rr
##############################################################################
# Send error to HTTP stream
def web_err(i):
"""
Input: {
http - http object
type - content type
bin - bytes to output
}
Output: {
return - 0
}
"""
http=i['http']
tp=i['type']
bin=i['bin']
try: bin=bin.decode('utf-8')
except Exception as e: pass
if tp=='json':
rx=ck.dumps_json({'dict':{'return':1, 'error':bin}})
if rx['return']>0:
bin2=rx['error'].encode('utf8')
else:
bin2=rx['string'].encode('utf-8')
elif tp=='con':
bin2=bin.encode('utf8')
else:
bin2=b'<html><body><pre>'+bin.encode('utf8')+b'</pre></body></html>'
i['bin']=bin2
return web_out(i)
##############################################################################
# Send error to HTTP stream
def web_out(i):
"""
Input: {
http - http object
type - content type
bin - bytes to output
(filename) - if !='', substitute filename in headers
}
Output: {
return - 0
}
"""
http=i['http']
bin=i['bin']
tp=i['type']
if tp=='' or tp=='web': tp='html'
tpx=cfg['content_types'].get(tp,{})
if len(tpx)==0:
tp='unknown'
tpx=cfg['content_types'][tp]
fn=i.get('filename','')
# Output
for k in sorted(tpx.keys()):
v=tpx[k]
if fn!='': v=v.replace('$#filename#$', fn)
http.send_header(k,v)
http.send_header('Content-Length', str(len(bin)))
http.end_headers()
http.wfile.write(bin)
return {'return':0}
##############################################################################
# Process CK web service request (both GET and POST)
def process_ck_web_request(i):
"""
Input: {
http - Python http object
}
Output: { None }
"""
# http object
http=i['http']
# Parse GET variables and path
xget={}
xpath={'host':'', 'port':'', 'first':'', 'rest':'', 'query':''} # May be used in the future
xt='json'
xpath['host']=i.get('host','')
xpath['port']=i.get('port','')
# Check GET variables
if http.path!='':
http.send_response(200)
a=urlparse.urlparse(http.path)
xp=a.path
xr=''
if xp.startswith('/'): xp=xp[1:]
u=xp.find('/')
if u>=0:
xr=xp[u+1:]
xp=xp[:u]
xt=xp
xpath['first']=xp
xpath['rest']=xr
xpath['query']=a.query
b=urlparse.parse_qs(a.query, keep_blank_values=True, )
xget={}
for k in b:
# xget[k]=b[k][0]
xget[k]=urlunquote(b[k][0])
if sys.version_info[0]<3:
xget[k]=xget[k].decode('utf8')
# Check POST
xpost={}
xpost1={}
try:
headers = http.headers
content_type = headers.get('content-type')
ctype=''
if content_type != None:
ctype, pdict = cgi.parse_header(content_type)
# Python3 cgi.parse_multipart expects boundary to be bytes, not str.
if sys.version_info[0]<3 and 'boundary' in pdict:
pdict['boundary'] = pdict['boundary'].encode()
if ctype == 'multipart/form-data':
if sys.version_info[0]<3:
xpost1 = cgi.parse_multipart(http.rfile, pdict)
else:
xxpost1 = cgi.FieldStorage(fp=http.rfile, headers=headers, environ={'REQUEST_METHOD':'POST'})
for k in xxpost1.keys():
xpost1[k]=[xxpost1[k].value]
elif ctype == 'application/x-www-form-urlencoded':
length = int(http.headers.get('content-length'))
s=http.rfile.read(length)
if sys.version_info[0]>2: s=s.decode('utf8')
xpost1 = cgi.parse_qs(s, keep_blank_values=1)
except Exception as e:
bin=b'internal CK web service error [7101] ('+format(e).encode('utf8')+')'
web_err({'http':http, 'type':xt, 'bin':bin})
ck.out(ck.cfg['error']+bin.decode('utf8'))
return
# Post processing
for k in xpost1:
v=xpost1[k]
if k.endswith('[]'):
k1=k[:-2]
xpost[k1]=[]
for l in v:
xpost[k1].append(urlunquote(l))
else:
if k!='file_content':
xpost[k]=urlunquote(v[0])
else:
xpost[k]=v[0]
if k=='file_content':
fcrt=xpost1.get('file_content_record_to_tmp','')
if (type(fcrt)==list and len(fcrt)>0 and fcrt[0]=='yes') or fcrt=='yes':
fd, fn=tempfile.mkstemp(suffix='.tmp', prefix='ck-') # suffix is important - CK will delete such file!
os.close(fd)
f=open(fn,'wb')
f.write(xpost[k])
f.close()
xpost[k+'_uploaded']=fn
del(xpost[k])
k+='_uploaded'
else:
import base64
xpost[k+'_base64']=base64.urlsafe_b64encode(xpost[k]).decode('utf8')
del(xpost[k])
k+='_base64'
if sys.version_info[0]<3:
xpost[k]=xpost[k].decode('utf8')
# Prepare input and check if CK json present
ii=xget
ii.update(xpost)
cj=ii.get('ck_json','').strip()
if cj!='':
r=ck.convert_json_str_to_dict({'str':cj, 'skip_quote_replacement':'yes'})
if r['return']>0:
bin=b'internal CK web service error [7102] ('+r['error'].encode('utf8')+b')'
web_err({'http':http, 'type':xt, 'bin':bin})
ck.out(ck.cfg['error']+bin.decode('utf8'))
return
del(ii['ck_json'])
ii.update(r['dict'])
# Misc parameters
dc=ii.get('detach_console','')
act=ii.get('action','')
# Check output type
if ii.get('out','')!='':
xt=ii['out']
if xt=='': xt='web'
if xt!='json' and xt!='con' and xt!='web':
web_out({'http':http,
'type':'web',
'bin':b'Unknown CK request ('+xt.encode('utf8')+b')!'})
return
# Prepare temporary output file
fd, fn=tempfile.mkstemp(prefix='ck-')
os.close(fd)
os.remove(fn)
# Check output
if dc=='yes':
if ck.cfg.get('forbid_detached_console','')=='yes':
web_out({'http':http,
'type':'web',
'bin':b'Detached console is forbidden!'})
return
else:
ii['out_file']=fn
ii['web']='yes'
if xt=='json' or xt=='web':
ii['out']='json_file'
# else output to console (for remote access for example)
ii['con_encoding']='utf8'
ii['host']=wfe_host
ii['port']=wfe_port
# Execute command *********************************************************
if act=='':
if cfg.get('if_web_action_not_defined','')!='' and cfg.get('if_web_module_not_defined','')!='':
ii['module_uoa']=cfg['if_web_module_not_defined']
ii['action']=cfg['if_web_action_not_defined']
r=call_ck(ii)
# Process output
if r['return']>0:
if os.path.isfile(fn): os.remove(fn)
bout=r['error']
try: bout=bout.encode('utf-8')
except Exception as e: pass
web_err({'http':http,
'type':xt,
'bin':bout})
return
# If output to console or detached console
if xt=='con' or dc=='yes':
if os.path.isfile(fn): os.remove(fn)
bout=r.get('std','').encode('utf8')
web_out({'http':http, 'type':xt, 'bin':bout})
return
# If json or web
# Try to load output file
if not os.path.isfile(fn):
web_err({'http':http,
'type':xt,
'bin':b'Output json file was not created, see output ('+r['std'].encode('utf8')+b')!'})
return
r=ck.load_text_file({'text_file':fn, 'keep_as_bin':'yes'})
if r['return']>0:
bout=r['error']
try: bout=bout.encode('utf-8')
except Exception as e: pass
web_err({'http':http, 'type':xt, 'bin':bout})
return
bin=r['bin']
if os.path.isfile(fn): os.remove(fn)
# Process JSON output from file
fx=''
if sys.version_info[0]>2: bin=bin.decode('utf-8')
ru=ck.convert_json_str_to_dict({'str':bin, 'skip_quote_replacement':'yes'})
if ru['return']>0:
bout=ru['error']
try: bout=bout.encode('utf-8')
except Exception as e: pass
web_err({'http':http, 'type':xt, 'bin':bout})
return
rr=ru['dict']
if rr['return']>0:
bout=rr['error']
try: bout=bout.encode('utf-8')
except Exception as e: pass
web_err({'http':http, 'type':xt, 'bin':bout})
return
# Check if file was returned
fr=False
if 'file_content_base64' in rr and rr.get('filename','')!='':
fr=True
# Check if download
if (xt=='web' and fr) or (act=='pull' and xt!='json'):
import base64
x=rr.get('file_content_base64','')
fx=rr.get('filename','')
if fx=='': fx=ck.cfg['default_archive_name']
# Fixing Python bug
if sys.version_info[0]==3 and sys.version_info[1]<3:
x=x.encode('utf-8')
else:
x=str(x)
bin=base64.urlsafe_b64decode(x) # convert from unicode to str since base64 works on strings
# should be safe in Python 2.x and 3.x
# Process extension
fn1, fne = os.path.splitext(fx)
if fne.startswith('.'): fne=fne[1:]
if fne!='': xt=fne
else: xt='unknown'
else:
# Check and output html
if rr.get('html','')!='':
bin=rr['html'].encode('utf-8')
else:
if sys.version_info[0]>2: # Unknown output
bin=bin.encode('utf-8')
web_out({'http':http, 'type':xt, 'bin':bin, 'filename':fx})
return {'return':0}
##############################################################################
# Class to handle requests in separate threads
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""
"""
##############################################################################
# Class to handle CK web service requests
class server_handler(BaseHTTPRequestHandler):
"""
Input: Python http handler
Output: None
"""
# Process only GET
def do_GET(self):
process_ck_web_request({'http':self})
return
# Process GET and POST
def do_POST(self):
process_ck_web_request({'http':self})
return
def log_request(self, code='-', size='-'):
self.log_message('"%s" %s %s', self.requestline, str(code), str(size))
return
def log_error(self, format, *args):
self.log_message(format, *args)
return
##############################################################################
# start web service
def start(i):
"""
Input: {
(host) - Internal web server host
(port) - Internal web server port
(wfe_host) - External web server host
(wfe_port) - External web server port
(browser) - if 'yes', open browser
(template) - if !='', add template
(wcid) - view a given entry
or
(cid)
(extra_url) - extra URL
}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
# Define internal server host.
host=ck.cfg.get('default_host','')
host=i.get('host',host)
if host=='': host='localhost' # 'localhost' if ''
# Define external server host.
global wfe_host
wfe_host=i.get('wfe_host',host)
# Define internal server port.
port=ck.cfg.get('default_port','')
port=i.get('port',port)
if port=='': return {'return':1, 'error':'web port is not defined'}
# Define external server port.
global wfe_port
wfe_port=i.get('wfe_port',port)
# Assemble URL.
url=host+':'+port
wfe_url=wfe_host+':'+wfe_port
ck.out('Starting CK web service on '+url+' (configured for access at '+wfe_url+') ...')
ck.out('')
sys.stdout.flush()
if i.get('browser','')=='yes':
rurl='http://'+url
ext=''
if i.get('template','')!='':
ext='template='+i['template']
cid=i.get('wcid','')
if cid=='':
cid=i.get('cid','')
if cid!='' and cid!='web':
if ext!='': ext+='&'
ext+='wcid='+cid
if i.get('extra_url','')!='':
if ext!='': ext+='&'
ext+=i['extra_url']
if ext!='':
rurl+='/?'+ext
import webbrowser
webbrowser.open(rurl)
try:
server = ThreadedHTTPServer((host, int(port)), server_handler)
# Prevent issues with socket reuse
server.allow_reuse_address=True
server.serve_forever()
except KeyboardInterrupt:
ck.out('Keyboard interrupt, terminating CK web service ...')
server.socket.close()
return {'return':0}
except OSError as e:
return {'return':1, 'error':'problem starting CK web service ('+format(e)+')'}
return {'return':0}
##############################################################################
# test web
def test(i):
"""
Input: {}
Output: {
return - return code = 0, if successful
> 0, if error
(error) - error text if return > 0
}
"""
h='<B>Test CK web (with unicode)</B><BR><BR>'
r=ck.access({'action':'load',
'module_uoa':'test',
'data_uoa':'unicode'})
if r['return']>0: return r
d=r['dict']
for q in d['languages']:
h+=q+'<BR>'
return {'return':0, 'html':h}
|