Skip to content

Commit f2b7d56

Browse files
authored
Merge branch 'master' into master
2 parents 3671198 + cf33271 commit f2b7d56

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

tika/tika.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def make_content_disposition_header(fn):
143143
import signal
144144
import logging
145145
import io
146+
import ctypes
146147

147148
log_path = os.getenv('TIKA_LOG_PATH', tempfile.gettempdir())
148149
log_file = os.path.join(log_path, 'tika.log')
@@ -475,7 +476,7 @@ def detectType1(option, urlOrPath, serverEndpoint=ServerEndpoint, verbose=Verbos
475476
status, response = callServer('put', serverEndpoint, service, open(path, 'rb'),
476477
{
477478
'Accept': responseMimeType,
478-
'Content-Disposition': make_content_disposition_header(path)
479+
'Content-Disposition': make_content_disposition_header(path.encode('utf-8') if type(path) is unicode_string else path)
479480
},
480481
verbose, tikaServerJar, config_path=config_path, requestOptions=requestOptions)
481482
if csvOutput == 1:
@@ -665,9 +666,15 @@ def startServer(tikaServerJar, java_path = TikaJava, java_args = TikaJavaArgs, s
665666

666667
# Run java with jar args
667668
global TikaServerProcess
669+
# Patch for Windows support
668670
if Windows:
669-
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, start_new_session=True)
670-
else:
671+
if sys.version.startswith("2"):
672+
# Python 2.x
673+
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True)
674+
elif sys.version.startswith("3"):
675+
# Python 3.x
676+
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, start_new_session=True)
677+
else:
671678
TikaServerProcess = Popen(cmd_string, stdout=logFile, stderr=STDOUT, shell=True, preexec_fn=os.setsid)
672679

673680
# Check logs and retry as configured
@@ -699,6 +706,25 @@ def killServer():
699706
except:
700707
log.error("Failed to kill the current server session")
701708
time.sleep(1)
709+
# patch to support subprocess killing for windows
710+
if Windows:
711+
if sys.version.startswith("2"):
712+
# Python 2.x
713+
PROCESS_TERMINATE = 1
714+
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, TikaServerProcess.pid)
715+
ctypes.windll.kernel32.TerminateProcess(handle, -1)
716+
ctypes.windll.kernel32.CloseHandle(handle)
717+
time.sleep(1)
718+
elif sys.version.startswith("3"):
719+
# Python 3.x
720+
os.kill(TikaServerProcess.pid, signal.SIGTERM)
721+
time.sleep(1)
722+
else:
723+
try:
724+
os.killpg(os.getpgid(TikaServerProcess.pid), signal.SIGTERM)
725+
except:
726+
log.error("Failed to kill the current server session")
727+
time.sleep(1)
702728
else:
703729
log.error("Server not running, or was already running before")
704730

0 commit comments

Comments
 (0)