def render_GET(self, request): """Retrieve a ReCaptcha from the API server and serve it to the client. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object for a page which should be protected by a CAPTCHA. :rtype: str :returns: A rendered HTML page containing a ReCaptcha challenge image for the client to solve. """ self.setCSPHeader(request) rtl = False image, challenge = self.getCaptchaImage(request) try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) # TODO: this does not work for versions of IE < 8.0 imgstr = 'data:image/jpeg;base64,%s' % base64.b64encode(image) template = lookup.get_template('captcha.html') rendered = template.render(strings, rtl=rtl, lang=langs[0], imgstr=imgstr, challenge_field=challenge) except Exception as err: rendered = replaceErrorPage(request, err, 'captcha.html') request.setHeader("Content-Type", "text/html; charset=utf-8") return rendered
def render_GET(self, request): """Retrieve a ReCaptcha from the API server and serve it to the client. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object for a page which should be protected by a CAPTCHA. :rtype: str :returns: A rendered HTML page containing a ReCaptcha challenge image for the client to solve. """ rtl = False image, challenge = self.getCaptchaImage(request) try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) # TODO: this does not work for versions of IE < 8.0 imgstr = 'data:image/jpeg;base64,%s' % base64.b64encode(image) template = lookup.get_template('captcha.html') rendered = template.render(strings, rtl=rtl, lang=langs[0], imgstr=imgstr, challenge_field=challenge) except Exception as err: rendered = replaceErrorPage(err, 'captcha.html') request.setHeader("Content-Type", "text/html; charset=utf-8") return rendered
def renderAnswer(self, request, bridgeLines=None): """Generate a response for a client which includes **bridgesLines**. .. note: The generated response can be plain or HTML. A plain response looks like:: voltron 1.2.3.4:1234 ABCDEF01234567890ABCDEF01234567890ABCDEF voltron 5.5.5.5:5555 0123456789ABCDEF0123456789ABCDEF01234567 That is, there is no HTML, what you see is what you get, and what you get is suitable for pasting directly into Tor Launcher (or into a torrc, if you prepend ``"Bridge "`` to each line). The plain format can be requested from BridgeDB's web service by adding an ``&format=plain`` HTTP GET parameter to the URL. Also note that you won't get a QRCode, usage instructions, error messages, or any other fanciness if you use the plain format. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object containing the HTTP method, full URI, and any URL/POST arguments and headers present. :type bridgeLines: list or None :param bridgeLines: A list of strings used to configure a Tor client to use a bridge. If ``None``, then the returned page will instead explain that there were no bridges of the type they requested, with instructions on how to proceed. :rtype: str :returns: A plaintext or HTML response to serve. """ rtl = False format = self.getResponseFormat(request) if format == 'plain': request.setHeader("Content-Type", "text/plain") try: rendered = bytes('\n'.join(bridgeLines)) except Exception as err: rendered = replaceErrorPage(request, err, html=False) else: request.setHeader("Content-Type", "text/html; charset=utf-8") qrcode = None qrjpeg = generateQR(bridgeLines) if qrjpeg: qrcode = 'data:image/jpeg;base64,%s' % base64.b64encode(qrjpeg) try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) template = lookup.get_template('bridges.html') rendered = template.render(strings, rtl=rtl, lang=langs[0], answer=bridgeLines, qrcode=qrcode) except Exception as err: rendered = replaceErrorPage(request, err) return rendered
def render_GET(self, request): rtl = False try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) template = lookup.get_template('howto.html') rendered = template.render(strings, rtl=rtl, lang=langs[0]) except Exception as err: # pragma: no cover rendered = replaceErrorPage(err) request.setHeader("Content-Type", "text/html; charset=utf-8") return rendered
def renderAnswer(self, request, bridgeLines=None, format=None): """Generate a response for a client which includes **bridges**. The generated response can be plaintext or HTML. :type request: :api:`twisted.web.http.Request` :param request: A ``Request`` object containing the HTTP method, full URI, and any URL/POST arguments and headers present. :type bridgeLines: list or None :param bridgeLines: A list of strings used to configure a Tor client to use a bridge. :type format: str or None :param format: If ``'plain'``, return a plaintext response. Otherwise, use the :file:`bridgedb/templates/bridges.html` template to render an HTML response page which includes the **bridges**. :rtype: str :returns: A plaintext or HTML response to serve. """ rtl = False if format == 'plain': request.setHeader("Content-Type", "text/plain") rendered = bridgeLines else: request.setHeader("Content-Type", "text/html; charset=utf-8") qrcode = None qrjpeg = generateQR(bridgeLines) if qrjpeg: qrcode = 'data:image/jpeg;base64,%s' % base64.b64encode(qrjpeg) try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) template = lookup.get_template('bridges.html') rendered = template.render(strings, rtl=rtl, lang=langs[0], answer=bridgeLines, qrcode=qrcode) except Exception as err: rendered = replaceErrorPage(err) return rendered
def render_GET(self, request): """Handles requests for the webserver root document. For example, this function handles requests for https://bridges.torproject.org/. :type request: :api:`twisted.web.server.Request` :param request: An incoming request. """ rtl = False try: langs = translations.getLocaleFromHTTPRequest(request) rtl = translations.usingRTLLang(langs) template = lookup.get_template('index.html') rendered = template.render(strings, rtl=rtl, lang=langs[0]) except Exception as err: rendered = replaceErrorPage(err) return rendered
def test_usingRTLLang(self): self.assertFalse(translations.usingRTLLang(['foo_BAR'])) self.assertFalse(translations.usingRTLLang(['en'])) if 'fa' in _langs.get_langs(): self.assertTrue(translations.usingRTLLang(['fa']))