Skip to content

Commit

Permalink
Feature/use if user asks for it (#135)
Browse files Browse the repository at this point in the history
* ntlm auth position
* dothttp is built on requests library. current changes are related to async reading and writing only
* Bump waitress from 1.4.3 to 2.1.1

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
cedric05 and dependabot[bot] authored Apr 8, 2022
1 parent 291d57e commit 65b23d5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
14 changes: 9 additions & 5 deletions dotextensions/server/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import asyncio
import logging
import sys

from dothttp.log_utils import setup_logging as root_logging_setup
from dothttp.__version__ import __version__
from .server import CmdServer, HttpServer
from .server import AsyncCmdServer, CmdServer, HttpServer


def setup_logging(level):
Expand All @@ -18,10 +19,8 @@ def main():
if len(sys.argv) == 2:
type_of_server = sys.argv[1]
else:
type_of_server = "cmd"
if type_of_server == "cmd":
CmdServer().run_forever()
elif type_of_server == "http":
type_of_server = 'cmd'
if type_of_server == "http":
port = 5000
if len(sys.argv) == 3:
try:
Expand All @@ -31,6 +30,11 @@ def main():
HttpServer(port).run_forever()
elif type_of_server == "version":
print(__version__)
elif type_of_server == "async":
print('async')
asyncio.run(AsyncCmdServer().run_forever())
elif type_of_server == "cmd":
CmdServer().run_forever()
else:
sys.exit(1)
sys.exit(0)
Expand Down
3 changes: 3 additions & 0 deletions dotextensions/server/handlers/gohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def pick_in_http(pick_http: Http, position: int) -> DothttpTypes:
elif pick_http.authwrap.digest_auth:
if self.is_in_between(pick_http.authwrap.digest_auth, position):
return DothttpTypes.DIGEST_AUTH
elif pick_http.authwrap.ntlm_auth:
if self.is_in_between(pick_http.authwrap.digest_auth, position):
return DothttpTypes.NTLM_AUTH
if certificate := pick_http.certificate:
if self.is_in_between(certificate, position):
return DothttpTypes.CERTIFICATE
Expand Down
1 change: 1 addition & 0 deletions dotextensions/server/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DothttpTypes(Enum):
URL = "url"
BASIC_AUTH = "basic_auth"
DIGEST_AUTH = "digest_auth"
NTLM_AUTH = "ntlm_auth"
CERTIFICATE = "certificate"
HEADER = "header"
URL_PARAMS = "urlparams"
Expand Down
47 changes: 47 additions & 0 deletions dotextensions/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
from json import JSONDecodeError
from typing import Dict
import asyncio
import typing

from dothttp.__version__ import __version__ as version
from .handlers.basic_handlers import RunHttpFileHandler, ContentExecuteHandler, FormatHttpFileHandler, \
Expand Down Expand Up @@ -108,3 +110,48 @@ def write_result(self, result):
def get_command(self, line):
output = json.loads(line)
return super().get_command(**output)

async def async_read_stdin() -> str:
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, sys.stdin.readline)

async def connect_stdin_stdout() -> typing.Tuple[asyncio.StreamReader, asyncio.StreamWriter]:
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader()
protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
w_transport, w_protocol = await loop.connect_write_pipe(asyncio.streams.FlowControlMixin, sys.stdout)
writer = asyncio.StreamWriter(w_transport, w_protocol, reader, loop)
return reader, writer

class AsyncCmdServer(CmdServer):

async def run_forever(self):
self.reader, self.writer = await connect_stdin_stdout()
# publish version
self.write_result({"id": -1, "result": {"dothttp_version": version}})
while True:
line = await self.reader.readline()
try:
logger.debug(f"got request {line}")
command = self.get_command(line)
if len(line) != 0:
await self.run_respond(command)
except JSONDecodeError:
logger.info(
f"input line `{line.strip()}` is not json decodable")
self.write_result(
{"id": 0, "result": {"error": True, "error_message": "not json decodable"}})
except Exception as e:
logger.info(
f"unknown exception `{e}` happened ", exc_info=True)
self.write_result(
{"id": 0, "result": {"error": True, "error_message": "not json decodable"}})

async def run_respond(self, command):
result = run(command)
self.write_result(result)

def write_result(self, result):
string_result = json.dumps(result) + "\n"
self.writer.write(string_result.encode())
2 changes: 1 addition & 1 deletion dothttp/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.40a7'
__version__ = '0.0.40'
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ pyparsing==2.1.10
pytest==6.2.3
pytest-cache==1.0
pytest-pep8==1.0.2
waitress==1.4.3
waitress==2.1.1
flask

0 comments on commit 65b23d5

Please sign in to comment.