Skip to content

Commit

Permalink
Merge pull request #5790 from RasaHQ/duckling-slash
Browse files Browse the repository at this point in the history
use concat_url to add "/parse" to duckling url safely
  • Loading branch information
erohmensing committed May 11, 2020
2 parents c847f11 + 51f63de commit 2e1c0ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelog/5792.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed issue where the ``DucklingHTTPExtractor`` component would
not work if its `url` contained a trailing slash.
18 changes: 13 additions & 5 deletions rasa/nlu/extractors/duckling_http_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
from typing import Any, List, Optional, Text, Dict

import rasa.utils.endpoints as endpoints_utils
from rasa.constants import DOCS_URL_COMPONENTS
from rasa.nlu.constants import ENTITIES
from rasa.nlu.config import RasaNLUModelConfig
Expand Down Expand Up @@ -113,15 +114,23 @@ def _payload(self, text: Text, reference_time: int) -> Dict[Text, Any]:
}

def _duckling_parse(self, text: Text, reference_time: int) -> List[Dict[Text, Any]]:
"""Sends the request to the duckling server and parses the result."""
"""Sends the request to the duckling server and parses the result.
Args:
text: Text for duckling server to parse.
reference_time: Reference time in milliseconds.
Returns:
JSON response from duckling server with parse data.
"""
parse_url = endpoints_utils.concat_url(self._url(), "/parse")
try:
payload = self._payload(text, reference_time)
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}
response = requests.post(
self._url() + "/parse",
parse_url,
data=payload,
headers=headers,
timeout=self.component_config.get("timeout"),
Expand All @@ -130,9 +139,8 @@ def _duckling_parse(self, text: Text, reference_time: int) -> List[Dict[Text, An
return response.json()
else:
logger.error(
"Failed to get a proper response from remote "
"duckling. Status Code: {}. Response: {}"
"".format(response.status_code, response.text)
f"Failed to get a proper response from remote "
f"duckling at '{parse_url}. Status Code: {response.status_code}. Response: {response.text}"
)
return []
except (
Expand Down
20 changes: 12 additions & 8 deletions rasa/utils/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import aiohttp
import logging
import os

import aiohttp
from aiohttp.client_exceptions import ContentTypeError
from typing import Any, Optional, Text, Dict

from sanic.request import Request
from typing import Any, Optional, Text, Dict

import rasa.utils.io
from rasa.constants import DEFAULT_REQUEST_TIMEOUT
Expand Down Expand Up @@ -44,14 +42,20 @@ def concat_url(base: Text, subpath: Optional[Text]) -> Text:
Strips leading slashes from the subpath if necessary. This behaves
differently than `urlparse.urljoin` and will not treat the subpath
as a base url if it starts with `/` but will always append it to the
`base`."""
`base`.
Args:
base: Base URL.
subpath: Optional path to append to the base URL.
Returns:
Concatenated URL with base and subpath.
"""
if not subpath:
if base.endswith("/"):
logger.debug(
"The URL '{}' has a trailing slash. Please make sure the "
"target server supports trailing slashes for this "
"endpoint.".format(base)
f"The URL '{base}' has a trailing slash. Please make sure the "
f"target server supports trailing slashes for this endpoint."
)
return base

Expand Down
10 changes: 10 additions & 0 deletions tests/utils/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
("https://example.com/", None, "https://example.com/"),
("https://example.com/", "test", "https://example.com/test"),
("https://example.com/", "test/", "https://example.com/test/"),
(
"http://duckling.rasa.com:8000",
"/parse",
"http://duckling.rasa.com:8000/parse",
),
(
"http://duckling.rasa.com:8000/",
"/parse",
"http://duckling.rasa.com:8000/parse",
),
],
)
def test_concat_url(base, subpath, expected_result):
Expand Down

0 comments on commit 2e1c0ff

Please sign in to comment.