w3lib-1.22.0/0000775000372000037200000000000013657045265013473 5ustar travistravis00000000000000w3lib-1.22.0/LICENSE0000664000372000037200000000277313657045223014503 0ustar travistravis00000000000000Copyright (c) w3lib and Scrapy developers. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Scrapy nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. w3lib-1.22.0/MANIFEST.in0000664000372000037200000000037213657045223015225 0ustar travistravis00000000000000# Include tests into distribution recursive-include tests *.py *.txt # Include documentation source recursive-include docs Makefile make.bat conf.py *.rst # Miscellaneous assets include LICENSE include README.rst include pytest.ini include tox.ini w3lib-1.22.0/PKG-INFO0000664000372000037200000000170213657045265014570 0ustar travistravis00000000000000Metadata-Version: 1.1 Name: w3lib Version: 1.22.0 Summary: Library of web-related functions Home-page: https://github.com/scrapy/w3lib Author: Scrapy project Author-email: info@scrapy.org License: BSD Description: UNKNOWN Platform: Any Classifier: Development Status :: 5 - Production/Stable Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Internet :: WWW/HTTP w3lib-1.22.0/README.rst0000664000372000037200000000162013657045223015153 0ustar travistravis00000000000000===== w3lib ===== .. image:: https://secure.travis-ci.org/scrapy/w3lib.png?branch=master :target: http://travis-ci.org/scrapy/w3lib .. image:: https://img.shields.io/codecov/c/github/scrapy/w3lib/master.svg :target: http://codecov.io/github/scrapy/w3lib?branch=master :alt: Coverage report Overview ======== This is a Python library of web-related functions, such as: * remove comments, or tags from HTML snippets * extract base url from HTML snippets * translate entites on HTML strings * convert raw HTTP headers to dicts and vice-versa * construct HTTP auth header * converting HTML pages to unicode * sanitize urls (like browsers do) * extract arguments from urls Requirements ============ Python 2.7 or Python 3.5+ Install ======= ``pip install w3lib`` Documentation ============= See http://w3lib.readthedocs.org/ License ======= The w3lib library is licensed under the BSD license. w3lib-1.22.0/docs/0000775000372000037200000000000013657045265014423 5ustar travistravis00000000000000w3lib-1.22.0/docs/Makefile0000664000372000037200000001267013657045223016063 0ustar travistravis00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" clean: -rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/w3lib.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/w3lib.qhc" devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/w3lib" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/w3lib" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." w3lib-1.22.0/docs/conf.py0000664000372000037200000002040313657045223015713 0ustar travistravis00000000000000# -*- coding: utf-8 -*- # # w3lib documentation build configuration file, created by # sphinx-quickstart on Sun Jan 26 22:19:38 2014. # # This file is execfile()d with the current directory set to its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.abspath('..')) # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ 'hoverxref.extension', 'notfound.extension', 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.viewcode', ] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = u'w3lib' copyright = u'2014, w3lib developers' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The full version, including alpha/beta/rc tags. release = '1.22.0' # The short X.Y version. version = '.'.join(release.split('.')[:2]) # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. #language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. html_theme = 'default' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'w3libdoc' # -- Options for LaTeX output -------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ ('index', 'w3lib.tex', u'w3lib Documentation', u'w3lib developers', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output -------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ ('index', 'w3lib', u'w3lib Documentation', [u'w3lib developers'], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------------ # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ ('index', 'w3lib', u'w3lib Documentation', u'w3lib developers', 'w3lib', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = { 'pytest': ('https://docs.pytest.org/en/latest', None), 'python': ('https://docs.python.org/3', None), 'scrapy': ('https://scrapy.readthedocs.io/en/latest', None), 'tox': ('https://tox.readthedocs.io/en/latest', None), } # -- Nitpicking options ------------------------------------------------------- nitpicky = True # -- sphinx-hoverxref options ------------------------------------------------- hoverxref_auto_ref = True w3lib-1.22.0/docs/index.rst0000664000372000037200000000270613657045223016263 0ustar travistravis00000000000000Welcome to w3lib's documentation! ================================= Overview ======== This is a Python library of web-related functions, such as: * remove comments, or tags from HTML snippets * extract base url from HTML snippets * translate entities on HTML strings * convert raw HTTP headers to dicts and vice-versa * construct HTTP auth header * converting HTML pages to unicode * sanitize urls (like browsers do) * extract arguments from urls The w3lib library is licensed under the BSD license. Modules ======= .. toctree:: :maxdepth: 4 w3lib Requirements ============ Python 2.7 or Python 3.3+ Install ======= ``pip install w3lib`` Tests ===== :doc:`pytest ` is the preferred way to run tests. Just run: ``pytest`` from the root directory to execute tests using the default Python interpreter. :doc:`tox ` could be used to run tests for all supported Python versions. Install it (using 'pip install tox') and then run ``tox`` from the root directory - tests will be executed for all available Python interpreters. Changelog ========= .. include:: ../NEWS :start-line: 3 History ------- The code of w3lib was originally part of the :doc:`Scrapy framework ` but was later stripped out of Scrapy, with the aim of make it more reusable and to provide a useful library of web functions without depending on Scrapy. Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` w3lib-1.22.0/docs/make.bat0000664000372000037200000001174613657045223016033 0ustar travistravis00000000000000@ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\w3lib.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\w3lib.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) :end w3lib-1.22.0/docs/w3lib.rst0000664000372000037200000000064313657045223016172 0ustar travistravis00000000000000w3lib Package ============= :mod:`~w3lib.encoding` Module ----------------------------- .. automodule:: w3lib.encoding :members: :mod:`~w3lib.html` Module ------------------------- .. automodule:: w3lib.html :members: :mod:`~w3lib.http` Module ------------------------- .. automodule:: w3lib.http :members: :mod:`~w3lib.url` Module ------------------------ .. automodule:: w3lib.url :members: w3lib-1.22.0/pytest.ini0000664000372000037200000000114113657045223015513 0ustar travistravis00000000000000[pytest] doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES flake8-ignore = docs/conf.py E121 E122 E265 E401 E501 tests/test_encoding.py E128 E221 E241 E302 E401 E501 E731 tests/test_form.py E265 E501 tests/test_html.py E123 E128 E241 E303 E501 E502 tests/test_http.py E128 E261 E302 W291 tests/test_url.py E126 E127 E128 E226 E261 E303 E501 W293 W391 w3lib/encoding.py E126 E128 E302 E305 E401 E501 w3lib/form.py E402 E501 E721 w3lib/html.py E128 E302 E501 E502 W504 w3lib/http.py E501 w3lib/url.py E128 E261 E302 E305 E501 F841 W291 W293 W504 w3lib/util.py E302 w3lib-1.22.0/setup.cfg0000664000372000037200000000010313657045265015306 0ustar travistravis00000000000000[bdist_wheel] universal = 1 [egg_info] tag_build = tag_date = 0 w3lib-1.22.0/setup.py0000664000372000037200000000223613657045223015202 0ustar travistravis00000000000000from setuptools import setup, find_packages setup( name='w3lib', version='1.22.0', license='BSD', description='Library of web-related functions', author='Scrapy project', author_email='info@scrapy.org', url='https://github.com/scrapy/w3lib', packages=find_packages(exclude=('tests', 'tests.*')), include_package_data=True, zip_safe=False, platforms=['Any'], classifiers=[ 'Development Status :: 5 - Production/Stable', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: Implementation :: CPython', 'Programming Language :: Python :: Implementation :: PyPy', 'Topic :: Internet :: WWW/HTTP', ], install_requires=['six >= 1.4.1'], ) w3lib-1.22.0/tests/0000775000372000037200000000000013657045265014635 5ustar travistravis00000000000000w3lib-1.22.0/tests/__init__.py0000664000372000037200000000000013657045223016726 0ustar travistravis00000000000000w3lib-1.22.0/tests/py3-ignores.txt0000664000372000037200000000011113657045223017540 0ustar travistravis00000000000000w3lib/encoding.py w3lib/form.py w3lib/html.py w3lib/http.py w3lib/url.py w3lib-1.22.0/tests/test_encoding.py0000664000372000037200000002761013657045223020034 0ustar travistravis00000000000000import unittest, codecs import six from w3lib.encoding import (html_body_declared_encoding, read_bom, to_unicode, http_content_type_encoding, resolve_encoding, html_to_unicode) class RequestEncodingTests(unittest.TestCase): utf8_fragments = [ # Content-Type as meta http-equiv b"""""", b"""\n""", b"""""", b"""""", b"""""", b"""""", b""" bad html still supported < meta http-equiv='Content-Type'\n content="text/html; charset=utf-8">""", # html5 meta charset b"""""", b"""""", # xml encoding b"""""", ] def test_bom(self): # cjk water character in unicode water_unicode = u'\u6C34' # BOM + water character encoded utf16be = b'\xfe\xff\x6c\x34' utf16le = b'\xff\xfe\x34\x6c' utf32be = b'\x00\x00\xfe\xff\x00\x00\x6c\x34' utf32le = b'\xff\xfe\x00\x00\x34\x6c\x00\x00' for string in (utf16be, utf16le, utf32be, utf32le): bom_encoding, bom = read_bom(string) decoded = string[len(bom):].decode(bom_encoding) self.assertEqual(water_unicode, decoded) # Body without BOM enc, bom = read_bom("foo") self.assertEqual(enc, None) self.assertEqual(bom, None) # Empty body enc, bom = read_bom("") self.assertEqual(enc, None) self.assertEqual(bom, None) def test_http_encoding_header(self): header_value = "Content-Type: text/html; charset=ISO-8859-4" extracted = http_content_type_encoding(header_value) self.assertEqual(extracted, "iso8859-4") self.assertEqual(None, http_content_type_encoding("something else")) def test_html_body_declared_encoding(self): for fragment in self.utf8_fragments: encoding = html_body_declared_encoding(fragment) self.assertEqual(encoding, 'utf-8', fragment) self.assertEqual(None, html_body_declared_encoding(b"something else")) self.assertEqual(None, html_body_declared_encoding(b""" this isn't searched """)) self.assertEqual(None, html_body_declared_encoding( b"""""")) def test_html_body_declared_encoding_unicode(self): # html_body_declared_encoding should work when unicode body is passed self.assertEqual(None, html_body_declared_encoding(u"something else")) for fragment in self.utf8_fragments: encoding = html_body_declared_encoding(fragment.decode('utf8')) self.assertEqual(encoding, 'utf-8', fragment) self.assertEqual(None, html_body_declared_encoding(u""" this isn't searched """)) self.assertEqual(None, html_body_declared_encoding( u"""""")) class CodecsEncodingTestCase(unittest.TestCase): def test_resolve_encoding(self): self.assertEqual(resolve_encoding('latin1'), 'cp1252') self.assertEqual(resolve_encoding(' Latin-1'), 'cp1252') self.assertEqual(resolve_encoding('gb_2312-80'), 'gb18030') self.assertEqual(resolve_encoding('unknown encoding'), None) class UnicodeDecodingTestCase(unittest.TestCase): def test_utf8(self): self.assertEqual(to_unicode(b'\xc2\xa3', 'utf-8'), u'\xa3') def test_invalid_utf8(self): self.assertEqual(to_unicode(b'\xc2\xc2\xa3', 'utf-8'), u'\ufffd\xa3') def ct(charset): return "Content-Type: text/html; charset=" + charset if charset else None def norm_encoding(enc): return codecs.lookup(enc).name class HtmlConversionTests(unittest.TestCase): def test_unicode_body(self): unicode_string = u'\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0442\u0435\u043a\u0441\u0442' original_string = unicode_string.encode('cp1251') encoding, body_unicode = html_to_unicode(ct('cp1251'), original_string) # check body_as_unicode self.assertTrue(isinstance(body_unicode, six.text_type)) self.assertEqual(body_unicode, unicode_string) def _assert_encoding(self, content_type, body, expected_encoding, expected_unicode): assert not isinstance(body, six.text_type) encoding, body_unicode = html_to_unicode(ct(content_type), body) self.assertTrue(isinstance(body_unicode, six.text_type)) self.assertEqual(norm_encoding(encoding), norm_encoding(expected_encoding)) if isinstance(expected_unicode, six.string_types): self.assertEqual(body_unicode, expected_unicode) else: self.assertTrue( body_unicode in expected_unicode, "%s is not in %s" % (body_unicode, expected_unicode) ) def test_content_type_and_conversion(self): """Test content type header is interpreted and text converted as expected """ self._assert_encoding('utf-8', b"\xc2\xa3", 'utf-8', u"\xa3") # something like this in the scrapy tests - but that's invalid? # self._assert_encoding('', "\xa3", 'utf-8', u"\xa3") # iso-8859-1 is overridden to cp1252 self._assert_encoding('iso-8859-1', b"\xa3", 'cp1252', u"\xa3") self._assert_encoding('', b"\xc2\xa3", 'utf-8', u"\xa3") self._assert_encoding('none', b"\xc2\xa3", 'utf-8', u"\xa3") self._assert_encoding('gb2312', b"\xa8D", 'gb18030', u"\u2015") self._assert_encoding('gbk', b"\xa8D", 'gb18030', u"\u2015") self._assert_encoding('big5', b"\xf9\xda", 'big5hkscs', u"\u6052") def test_invalid_utf8_encoded_body_with_valid_utf8_BOM(self): # unlike scrapy, the BOM is stripped self._assert_encoding('utf-8', b"\xef\xbb\xbfWORD\xe3\xabWORD2", 'utf-8', u'WORD\ufffdWORD2') self._assert_encoding(None, b"\xef\xbb\xbfWORD\xe3\xabWORD2", 'utf-8', u'WORD\ufffdWORD2') def test_utf8_unexpected_end_of_data_with_valid_utf8_BOM(self): # Python implementations handle unexpected end of UTF8 data # differently (see https://bugs.pypy.org/issue1536). # It is hard to fix this for PyPy in w3lib, so the test # is permissive. # unlike scrapy, the BOM is stripped self._assert_encoding('utf-8', b"\xef\xbb\xbfWORD\xe3\xab", 'utf-8', [u'WORD\ufffd\ufffd', u'WORD\ufffd']) self._assert_encoding(None, b"\xef\xbb\xbfWORD\xe3\xab", 'utf-8', [u'WORD\ufffd\ufffd', u'WORD\ufffd']) def test_replace_wrong_encoding(self): """Test invalid chars are replaced properly""" encoding, body_unicode = html_to_unicode(ct('utf-8'), b'PREFIX\xe3\xabSUFFIX') # XXX: Policy for replacing invalid chars may suffer minor variations # but it should always contain the unicode replacement char (u'\ufffd') assert u'\ufffd' in body_unicode, repr(body_unicode) assert u'PREFIX' in body_unicode, repr(body_unicode) assert u'SUFFIX' in body_unicode, repr(body_unicode) # Do not destroy html tags due to encoding bugs encoding, body_unicode = html_to_unicode(ct('utf-8'), b'\xf0value') assert u'value' in body_unicode, repr(body_unicode) def _assert_encoding_detected(self, content_type, expected_encoding, body, **kwargs): assert not isinstance(body, six.text_type) encoding, body_unicode = html_to_unicode(ct(content_type), body, **kwargs) self.assertTrue(isinstance(body_unicode, six.text_type)) self.assertEqual(norm_encoding(encoding), norm_encoding(expected_encoding)) def test_BOM(self): # utf-16 cases already tested, as is the BOM detection function # http header takes precedence, irrespective of BOM bom_be_str = codecs.BOM_UTF16_BE + u"hi".encode('utf-16-be') expected = u'\ufffd\ufffd\x00h\x00i' self._assert_encoding('utf-8', bom_be_str, 'utf-8', expected) # BOM is stripped when it agrees with the encoding, or used to # determine encoding bom_utf8_str = codecs.BOM_UTF8 + b'hi' self._assert_encoding('utf-8', bom_utf8_str, 'utf-8', u"hi") self._assert_encoding(None, bom_utf8_str, 'utf-8', u"hi") def test_utf16_32(self): # tools.ietf.org/html/rfc2781 section 4.3 # USE BOM and strip it bom_be_str = codecs.BOM_UTF16_BE + u"hi".encode('utf-16-be') self._assert_encoding('utf-16', bom_be_str, 'utf-16-be', u"hi") self._assert_encoding(None, bom_be_str, 'utf-16-be', u"hi") bom_le_str = codecs.BOM_UTF16_LE + u"hi".encode('utf-16-le') self._assert_encoding('utf-16', bom_le_str, 'utf-16-le', u"hi") self._assert_encoding(None, bom_le_str, 'utf-16-le', u"hi") bom_be_str = codecs.BOM_UTF32_BE + u"hi".encode('utf-32-be') self._assert_encoding('utf-32', bom_be_str, 'utf-32-be', u"hi") self._assert_encoding(None, bom_be_str, 'utf-32-be', u"hi") bom_le_str = codecs.BOM_UTF32_LE + u"hi".encode('utf-32-le') self._assert_encoding('utf-32', bom_le_str, 'utf-32-le', u"hi") self._assert_encoding(None, bom_le_str, 'utf-32-le', u"hi") # if there is no BOM, big endian should be chosen self._assert_encoding('utf-16', u"hi".encode('utf-16-be'), 'utf-16-be', u"hi") self._assert_encoding('utf-32', u"hi".encode('utf-32-be'), 'utf-32-be', u"hi") def test_python_crash(self): import random from io import BytesIO random.seed(42) buf = BytesIO() for i in range(150000): buf.write(bytes([random.randint(0, 255)])) to_unicode(buf.getvalue(), 'utf-16-le') to_unicode(buf.getvalue(), 'utf-16-be') to_unicode(buf.getvalue(), 'utf-32-le') to_unicode(buf.getvalue(), 'utf-32-be') def test_html_encoding(self): # extracting the encoding from raw html is tested elsewhere body = b"""blah blah < meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> other stuff""" self._assert_encoding_detected(None, 'cp1252', body) # header encoding takes precedence self._assert_encoding_detected('utf-8', 'utf-8', body) # BOM encoding takes precedence self._assert_encoding_detected(None, 'utf-8', codecs.BOM_UTF8 + body) def test_autodetect(self): asciif = lambda x: 'ascii' body = b"""""" # body encoding takes precedence self._assert_encoding_detected(None, 'utf-8', body, auto_detect_fun=asciif) # if no other encoding, the auto detect encoding is used. self._assert_encoding_detected(None, 'ascii', b"no encoding info", auto_detect_fun=asciif) def test_default_encoding(self): # if no other method available, the default encoding of utf-8 is used self._assert_encoding_detected(None, 'utf-8', b"no encoding info") # this can be overridden self._assert_encoding_detected(None, 'ascii', b"no encoding info", default_encoding='ascii') def test_empty_body(self): # if no other method available, the default encoding of utf-8 is used self._assert_encoding_detected(None, 'utf-8', b"") w3lib-1.22.0/tests/test_form.py0000664000372000037200000000465113657045223017211 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import import warnings import unittest from collections import OrderedDict from w3lib.form import encode_multipart class EncodeMultipartTest(unittest.TestCase): def test_encode_multipart(self): data = {'key': 'value'} with warnings.catch_warnings(record=True): body, boundary = encode_multipart(data) expected_body = ( '\r\n--{boundary}' '\r\nContent-Disposition: form-data; name="key"\r\n' '\r\nvalue' '\r\n--{boundary}--' '\r\n'.format(boundary=boundary).encode('utf8') ) self.assertEqual(body, expected_body) def test_encode_multipart_unicode(self): data = OrderedDict([ (u'ключ1', u'значение1'.encode('utf8')), (u'ключ2', u'значение2'), ]) with warnings.catch_warnings(record=True): body, boundary = encode_multipart(data) expected_body = ( u'\r\n--{boundary}' u'\r\nContent-Disposition: form-data; name="ключ1"\r\n' u'\r\nзначение1' u'\r\n--{boundary}' u'\r\nContent-Disposition: form-data; name="ключ2"\r\n' u'\r\nзначение2' u'\r\n--{boundary}--' u'\r\n'.format(boundary=boundary).encode('utf8') ) self.assertEqual(body, expected_body) def test_encode_multipart_file(self): # this data is not decodable using utf8 data = {'key': ('file/name', b'\xa1\xa2\xa3\xa4\r\n\r')} with warnings.catch_warnings(record=True): body, boundary = encode_multipart(data) body_lines = [ b'\r\n--' + boundary.encode('ascii'), b'\r\nContent-Disposition: form-data; name="key"; filename="file/name"\r\n', b'\r\n\xa1\xa2\xa3\xa4\r\n\r', b'\r\n--' + boundary.encode('ascii') + b'--\r\n', ] expected_body = b''.join(body_lines) self.assertEqual(body, expected_body) #def test_encode_multipart_int(self): # data = {'key': 123} # body, boundary = encode_multipart2(data) # expected_body = ( # '\n--{boundary}' # '\nContent-Disposition: form-data; name="key"\n' # '\n123' # '\n--{boundary}--' # '\n'.format(boundary=boundary) # ) # self.assertEqual(body, expected_body) w3lib-1.22.0/tests/test_html.py0000664000372000037200000005362313657045223017215 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import unittest import six from w3lib.html import (replace_entities, replace_tags, remove_comments, remove_tags_with_content, replace_escape_chars, remove_tags, unquote_markup, get_base_url, get_meta_refresh) class RemoveEntitiesTest(unittest.TestCase): def test_returns_unicode(self): # make sure it always return uncode assert isinstance(replace_entities(b'no entities'), six.text_type) assert isinstance(replace_entities(b'Price: £100!'), six.text_type) assert isinstance(replace_entities(u'no entities'), six.text_type) assert isinstance(replace_entities(u'Price: £100!'), six.text_type) def test_regular(self): # regular conversions self.assertEqual(replace_entities(u'As low as £100!'), u'As low as \xa3100!') self.assertEqual(replace_entities(b'As low as £100!'), u'As low as \xa3100!') self.assertEqual(replace_entities('redirectTo=search&searchtext=MR0221Y&aff=buyat&affsrc=d_data&cm_mmc=buyat-_-ELECTRICAL & SEASONAL-_-MR0221Y-_-9-carat gold ½oz solid crucifix pendant'), u'redirectTo=search&searchtext=MR0221Y&aff=buyat&affsrc=d_data&cm_mmc=buyat-_-ELECTRICAL & SEASONAL-_-MR0221Y-_-9-carat gold \xbdoz solid crucifix pendant') def test_keep_entities(self): # keep some entities self.assertEqual(replace_entities(b'Low < High & Medium £ six', keep=['lt', 'amp']), u'Low < High & Medium \xa3 six') self.assertEqual(replace_entities(u'Low < High & Medium £ six', keep=[u'lt', u'amp']), u'Low < High & Medium \xa3 six') def test_illegal_entities(self): self.assertEqual(replace_entities('a < b &illegal; c � six', remove_illegal=False), u'a < b &illegal; c � six') self.assertEqual(replace_entities('a < b &illegal; c � six', remove_illegal=True), u'a < b c six') self.assertEqual(replace_entities('x≤y'), u'x\u2264y') self.assertEqual(replace_entities('xy'), u'xy') self.assertEqual(replace_entities('xy', remove_illegal=False), u'xy') def test_browser_hack(self): # check browser hack for numeric character references in the 80-9F range self.assertEqual(replace_entities('x™y', encoding='cp1252'), u'x\u2122y') self.assertEqual(replace_entities('x™y', encoding='cp1252'), u'x\u2122y') def test_missing_semicolon(self): for entity, result in ( ('<<!', '<some tag'), u'This text contains some tag') self.assertEqual(replace_tags(b'This text is very important', ' '), u'This text is very im port ant') def test_replace_tags_multiline(self): self.assertEqual(replace_tags(b'Click here'), u'Click here') class RemoveCommentsTest(unittest.TestCase): def test_returns_unicode(self): # make sure it always return unicode assert isinstance(remove_comments(b'without comments'), six.text_type) assert isinstance(remove_comments(b''), six.text_type) assert isinstance(remove_comments(u'without comments'), six.text_type) assert isinstance(remove_comments(u''), six.text_type) def test_no_comments(self): # text without comments self.assertEqual(remove_comments(u'text without comments'), u'text without comments') def test_remove_comments(self): # text with comments self.assertEqual(remove_comments(u''), u'') self.assertEqual(remove_comments(u'Hello'), u'Hello') self.assertEqual(remove_comments(u'Hello'), u'Hello') self.assertEqual(remove_comments(b"test whatever"), u'test whatever') self.assertEqual(remove_comments(b"test whatever"), u'test whatever') self.assertEqual(remove_comments(b"test """ self.assertEqual(get_meta_refresh(body, baseurl), (None, None)) def test_html_comments_with_uncommented_meta_refresh(self): # html comments must not interfere with uncommented meta refresh header baseurl = 'http://example.com' body = """-->""" self.assertEqual(get_meta_refresh(body, baseurl), (3, 'http://example.com/')) def test_float_refresh_intervals(self): # float refresh intervals baseurl = 'http://example.com' body = """""" self.assertEqual(get_meta_refresh(body, baseurl), (0.1, 'http://example.com/index.html')) body = """""" self.assertEqual(get_meta_refresh(body, baseurl), (3.1, 'http://example.com/index.html')) def test_tag_name(self): baseurl = 'http://example.org' body = """ Dummy blahablsdfsal& """ self.assertEqual(get_meta_refresh(body, baseurl), (None, None)) def test_leading_newline_in_url(self): baseurl = 'http://example.org' body = """ Dummy """ self.assertEqual(get_meta_refresh(body, baseurl), (0.0, 'http://www.example.org/index.php')) def test_inside_noscript(self): baseurl = 'http://example.org' body = """ """ self.assertEqual(get_meta_refresh(body, baseurl), (None, None)) self.assertEqual(get_meta_refresh(body, baseurl, ignore_tags=()), (0.0, "http://example.org/javascript_required")) def test_inside_script(self): baseurl = 'http://example.org' body = """ """ self.assertEqual(get_meta_refresh(body, baseurl), (None, None)) self.assertEqual(get_meta_refresh(body, baseurl, ignore_tags=()), (0.0, "http://example.org/foobar_required")) w3lib-1.22.0/tests/test_http.py0000664000372000037200000000626613657045223017231 0ustar travistravis00000000000000# -*- coding: utf-8 -*- import unittest from collections import OrderedDict from w3lib.http import (basic_auth_header, headers_dict_to_raw, headers_raw_to_dict) __doctests__ = ['w3lib.http'] # for trial support class HttpTests(unittest.TestCase): def test_basic_auth_header(self): self.assertEqual(b'Basic c29tZXVzZXI6c29tZXBhc3M=', basic_auth_header('someuser', 'somepass')) # Check url unsafe encoded header self.assertEqual(b'Basic c29tZXVzZXI6QDx5dTk-Jm8_UQ==', basic_auth_header('someuser', '@&o?Q')) def test_basic_auth_header_encoding(self): self.assertEqual(b'Basic c29tw6Z1c8Oocjpzw7htZXDDpHNz', basic_auth_header(u'somæusèr', u'sømepäss', encoding='utf8')) # default encoding (ISO-8859-1) self.assertEqual(b'Basic c29t5nVz6HI6c_htZXDkc3M=', basic_auth_header(u'somæusèr', u'sømepäss')) def test_headers_raw_dict_none(self): self.assertIsNone(headers_raw_to_dict(None)) self.assertIsNone(headers_dict_to_raw(None)) def test_headers_raw_to_dict(self): raw = b"Content-type: text/html\n\rAccept: gzip\n\r\ Cache-Control: no-cache\n\rCache-Control: no-store\n\n" dct = {b'Content-type': [b'text/html'], b'Accept': [b'gzip'], b'Cache-Control': [b'no-cache', b'no-store']} self.assertEqual(headers_raw_to_dict(raw), dct) def test_headers_dict_to_raw(self): dct = OrderedDict([ (b'Content-type', b'text/html'), (b'Accept', b'gzip') ]) self.assertEqual( headers_dict_to_raw(dct), b'Content-type: text/html\r\nAccept: gzip' ) def test_headers_dict_to_raw_listtuple(self): dct = OrderedDict([ (b'Content-type', [b'text/html']), (b'Accept', [b'gzip']) ]) self.assertEqual( headers_dict_to_raw(dct), b'Content-type: text/html\r\nAccept: gzip' ) dct = OrderedDict([ (b'Content-type', (b'text/html',)), (b'Accept', (b'gzip',)) ]) self.assertEqual( headers_dict_to_raw(dct), b'Content-type: text/html\r\nAccept: gzip' ) dct = OrderedDict([ (b'Cookie', (b'val001', b'val002')), (b'Accept', b'gzip') ]) self.assertEqual( headers_dict_to_raw(dct), b'Cookie: val001\r\nCookie: val002\r\nAccept: gzip' ) dct = OrderedDict([ (b'Cookie', [b'val001', b'val002']), (b'Accept', b'gzip') ]) self.assertEqual( headers_dict_to_raw(dct), b'Cookie: val001\r\nCookie: val002\r\nAccept: gzip' ) def test_headers_dict_to_raw_wrong_values(self): dct = OrderedDict([ (b'Content-type', 0), ]) self.assertEqual( headers_dict_to_raw(dct), b'' ) dct = OrderedDict([ (b'Content-type', 1), (b'Accept', [b'gzip']) ]) self.assertEqual( headers_dict_to_raw(dct), b'Accept: gzip' ) w3lib-1.22.0/tests/test_url.py0000664000372000037200000012330113657045223017042 0ustar travistravis00000000000000# -*- coding: utf-8 -*- from __future__ import absolute_import import os import unittest from w3lib.url import (is_url, safe_url_string, safe_download_url, url_query_parameter, add_or_replace_parameter, url_query_cleaner, file_uri_to_path, parse_data_uri, path_to_file_uri, any_to_uri, urljoin_rfc, canonicalize_url, parse_url, add_or_replace_parameters) from six.moves.urllib.parse import urlparse class UrlTests(unittest.TestCase): def test_safe_url_string(self): # Motoko Kusanagi (Cyborg from Ghost in the Shell) motoko = u'\u8349\u8599 \u7d20\u5b50' self.assertEqual(safe_url_string(motoko), # note the %20 for space '%E8%8D%89%E8%96%99%20%E7%B4%A0%E5%AD%90') self.assertEqual(safe_url_string(motoko), safe_url_string(safe_url_string(motoko))) self.assertEqual(safe_url_string(u'©'), # copyright symbol '%C2%A9') # page-encoding does not affect URL path self.assertEqual(safe_url_string(u'©', 'iso-8859-1'), '%C2%A9') # path_encoding does self.assertEqual(safe_url_string(u'©', path_encoding='iso-8859-1'), '%A9') self.assertEqual(safe_url_string("http://www.example.org/"), 'http://www.example.org/') alessi = u'/ecommerce/oggetto/Te \xf2/tea-strainer/1273' self.assertEqual(safe_url_string(alessi), '/ecommerce/oggetto/Te%20%C3%B2/tea-strainer/1273') self.assertEqual(safe_url_string("http://www.example.com/test?p(29)url(http://www.another.net/page)"), "http://www.example.com/test?p(29)url(http://www.another.net/page)") self.assertEqual(safe_url_string("http://www.example.com/Brochures_&_Paint_Cards&PageSize=200"), "http://www.example.com/Brochures_&_Paint_Cards&PageSize=200") # page-encoding does not affect URL path # we still end up UTF-8 encoding characters before percent-escaping safeurl = safe_url_string(u"http://www.example.com/£") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3") safeurl = safe_url_string(u"http://www.example.com/£", encoding='utf-8') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3") safeurl = safe_url_string(u"http://www.example.com/£", encoding='latin-1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3") safeurl = safe_url_string(u"http://www.example.com/£", path_encoding='latin-1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%A3") self.assertTrue(isinstance(safe_url_string(b'http://example.com/'), str)) def test_safe_url_string_remove_ascii_tab_and_newlines(self): self.assertEqual(safe_url_string("http://example.com/test\n.html"), "http://example.com/test.html") self.assertEqual(safe_url_string("http://example.com/test\t.html"), "http://example.com/test.html") self.assertEqual(safe_url_string("http://example.com/test\r.html"), "http://example.com/test.html") self.assertEqual(safe_url_string("http://example.com/test\r.html\n"), "http://example.com/test.html") self.assertEqual(safe_url_string("http://example.com/test\r\n.html\t"), "http://example.com/test.html") self.assertEqual(safe_url_string("http://example.com/test\a\n.html"), "http://example.com/test%07.html") def test_safe_url_string_unsafe_chars(self): safeurl = safe_url_string(r"http://localhost:8001/unwise{,},|,\,^,[,],`?|=[]&[]=|") self.assertEqual(safeurl, r"http://localhost:8001/unwise%7B,%7D,|,%5C,%5E,[,],%60?|=[]&[]=|") def test_safe_url_string_quote_path(self): safeurl = safe_url_string(u'http://google.com/"hello"', quote_path=True) self.assertEqual(safeurl, u'http://google.com/%22hello%22') safeurl = safe_url_string(u'http://google.com/"hello"', quote_path=False) self.assertEqual(safeurl, u'http://google.com/"hello"') safeurl = safe_url_string(u'http://google.com/"hello"') self.assertEqual(safeurl, u'http://google.com/%22hello%22') def test_safe_url_string_with_query(self): safeurl = safe_url_string(u"http://www.example.com/£?unit=µ") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%C2%B5") safeurl = safe_url_string(u"http://www.example.com/£?unit=µ", encoding='utf-8') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%C2%B5") safeurl = safe_url_string(u"http://www.example.com/£?unit=µ", encoding='latin-1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%B5") safeurl = safe_url_string(u"http://www.example.com/£?unit=µ", path_encoding='latin-1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%A3?unit=%C2%B5") safeurl = safe_url_string(u"http://www.example.com/£?unit=µ", encoding='latin-1', path_encoding='latin-1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%A3?unit=%B5") def test_safe_url_string_misc(self): # mixing Unicode and percent-escaped sequences safeurl = safe_url_string(u"http://www.example.com/£?unit=%C2%B5") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%C2%B5") safeurl = safe_url_string(u"http://www.example.com/%C2%A3?unit=µ") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%C2%B5") def test_safe_url_string_bytes_input(self): safeurl = safe_url_string(b"http://www.example.com/") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/") # bytes input is assumed to be UTF-8 safeurl = safe_url_string(b"http://www.example.com/\xc2\xb5") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%B5") # page-encoding encoded bytes still end up as UTF-8 sequences in path safeurl = safe_url_string(b"http://www.example.com/\xb5", encoding='latin1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%B5") safeurl = safe_url_string(b"http://www.example.com/\xa3?unit=\xb5", encoding='latin1') self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%C2%A3?unit=%B5") def test_safe_url_string_bytes_input_nonutf8(self): # latin1 safeurl = safe_url_string(b"http://www.example.com/\xa3?unit=\xb5") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/%A3?unit=%B5") # cp1251 # >>> u'Россия'.encode('cp1251') # '\xd0\xee\xf1\xf1\xe8\xff' safeurl = safe_url_string(b"http://www.example.com/country/\xd0\xee\xf1\xf1\xe8\xff") self.assertTrue(isinstance(safeurl, str)) self.assertEqual(safeurl, "http://www.example.com/country/%D0%EE%F1%F1%E8%FF") def test_safe_url_idna(self): # adapted from: # https://ssl.icu-project.org/icu-bin/idnbrowser # http://unicode.org/faq/idn.html # + various others websites = ( (u'http://www.färgbolaget.nu/färgbolaget', 'http://www.xn--frgbolaget-q5a.nu/f%C3%A4rgbolaget'), (u'http://www.räksmörgås.se/?räksmörgås=yes', 'http://www.xn--rksmrgs-5wao1o.se/?r%C3%A4ksm%C3%B6rg%C3%A5s=yes'), (u'http://www.brændendekærlighed.com/brændende/kærlighed', 'http://www.xn--brndendekrlighed-vobh.com/br%C3%A6ndende/k%C3%A6rlighed'), (u'http://www.예비교사.com', 'http://www.xn--9d0bm53a3xbzui.com'), (u'http://理容ナカムラ.com', 'http://xn--lck1c3crb1723bpq4a.com'), (u'http://あーるいん.com', 'http://xn--l8je6s7a45b.com'), # --- real websites --- # in practice, this redirect (301) to http://www.buecher.de/?q=b%C3%BCcher (u'http://www.bücher.de/?q=bücher', 'http://www.xn--bcher-kva.de/?q=b%C3%BCcher'), # Japanese (u'http://はじめよう.みんな/?query=サ&maxResults=5', 'http://xn--p8j9a0d9c9a.xn--q9jyb4c/?query=%E3%82%B5&maxResults=5'), # Russian (u'http://кто.рф/', 'http://xn--j1ail.xn--p1ai/'), (u'http://кто.рф/index.php?domain=Что', 'http://xn--j1ail.xn--p1ai/index.php?domain=%D0%A7%D1%82%D0%BE'), # Korean (u'http://내도메인.한국/', 'http://xn--220b31d95hq8o.xn--3e0b707e/'), (u'http://맨체스터시티축구단.한국/', 'http://xn--2e0b17htvgtvj9haj53ccob62ni8d.xn--3e0b707e/'), # Arabic (u'http://nic.شبكة', 'http://nic.xn--ngbc5azd'), # Chinese (u'https://www.贷款.在线', 'https://www.xn--0kwr83e.xn--3ds443g'), (u'https://www2.xn--0kwr83e.在线', 'https://www2.xn--0kwr83e.xn--3ds443g'), (u'https://www3.贷款.xn--3ds443g', 'https://www3.xn--0kwr83e.xn--3ds443g'), ) for idn_input, safe_result in websites: safeurl = safe_url_string(idn_input) self.assertEqual(safeurl, safe_result) # make sure the safe URL is unchanged when made safe a 2nd time for _, safe_result in websites: safeurl = safe_url_string(safe_result) self.assertEqual(safeurl, safe_result) def test_safe_url_idna_encoding_failure(self): # missing DNS label self.assertEqual( safe_url_string(u"http://.example.com/résumé?q=résumé"), "http://.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") # DNS label too long self.assertEqual( safe_url_string( u"http://www.{label}.com/résumé?q=résumé".format( label=u"example"*11)), "http://www.{label}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9".format( label=u"example"*11)) def test_safe_url_port_number(self): self.assertEqual( safe_url_string(u"http://www.example.com:80/résumé?q=résumé"), "http://www.example.com:80/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") self.assertEqual( safe_url_string(u"http://www.example.com:/résumé?q=résumé"), "http://www.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") def test_safe_download_url(self): self.assertEqual(safe_download_url('http://www.example.org'), 'http://www.example.org/') self.assertEqual(safe_download_url('http://www.example.org/../'), 'http://www.example.org/') self.assertEqual(safe_download_url('http://www.example.org/../../images/../image'), 'http://www.example.org/image') self.assertEqual(safe_download_url('http://www.example.org/dir/'), 'http://www.example.org/dir/') self.assertEqual(safe_download_url(b'http://www.example.org/dir/'), 'http://www.example.org/dir/') # Encoding related tests self.assertEqual(safe_download_url(b'http://www.example.org?\xa3', encoding='latin-1', path_encoding='latin-1'), 'http://www.example.org/?%A3') self.assertEqual(safe_download_url(b'http://www.example.org?\xc2\xa3', encoding='utf-8', path_encoding='utf-8'), 'http://www.example.org/?%C2%A3') self.assertEqual(safe_download_url(b'http://www.example.org/\xc2\xa3?\xc2\xa3', encoding='utf-8', path_encoding='latin-1'), 'http://www.example.org/%A3?%C2%A3') def test_is_url(self): self.assertTrue(is_url('http://www.example.org')) self.assertTrue(is_url('https://www.example.org')) self.assertTrue(is_url('file:///some/path')) self.assertFalse(is_url('foo://bar')) self.assertFalse(is_url('foo--bar')) def test_url_query_parameter(self): self.assertEqual(url_query_parameter("product.html?id=200&foo=bar", "id"), '200') self.assertEqual(url_query_parameter("product.html?id=200&foo=bar", "notthere", "mydefault"), 'mydefault') self.assertEqual(url_query_parameter("product.html?id=", "id"), None) self.assertEqual(url_query_parameter("product.html?id=", "id", keep_blank_values=1), '') def test_url_query_parameter_2(self): """ This problem was seen several times in the feeds. Sometime affiliate URLs contains nested encoded affiliate URL with direct URL as parameters. For example: aff_url1 = 'http://www.tkqlhce.com/click-2590032-10294381?url=http%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FArgosCreateReferral%3FstoreId%3D10001%26langId%3D-1%26referrer%3DCOJUN%26params%3Dadref%253DGarden+and+DIY-%3EGarden+furniture-%3EChildren%26%2339%3Bs+garden+furniture%26referredURL%3Dhttp%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%253FstoreId%253D10001%2526catalogId%253D1500001501%2526productId%253D1500357023%2526langId%253D-1' the typical code to extract needed URL from it is: aff_url2 = url_query_parameter(aff_url1, 'url') after this aff2_url is: 'http://www.argos.co.uk/webapp/wcs/stores/servlet/ArgosCreateReferral?storeId=10001&langId=-1&referrer=COJUN¶ms=adref%3DGarden and DIY->Garden furniture->Children's gardenfurniture&referredURL=http://www.argos.co.uk/webapp/wcs/stores/servlet/ProductDisplay%3FstoreId%3D10001%26catalogId%3D1500001501%26productId%3D1500357023%26langId%3D-1' the direct URL extraction is url = url_query_parameter(aff_url2, 'referredURL') but this will not work, because aff_url2 contains ' (comma sign encoded in the feed) and the URL extraction will fail, current workaround was made in the spider, just a replace for ' to %27 """ return # FIXME: this test should pass but currently doesnt # correct case aff_url1 = "http://www.anrdoezrs.net/click-2590032-10294381?url=http%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FArgosCreateReferral%3FstoreId%3D10001%26langId%3D-1%26referrer%3DCOJUN%26params%3Dadref%253DGarden+and+DIY-%3EGarden+furniture-%3EGarden+table+and+chair+sets%26referredURL%3Dhttp%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%253FstoreId%253D10001%2526catalogId%253D1500001501%2526productId%253D1500357199%2526langId%253D-1" aff_url2 = url_query_parameter(aff_url1, 'url') self.assertEqual(aff_url2, "http://www.argos.co.uk/webapp/wcs/stores/servlet/ArgosCreateReferral?storeId=10001&langId=-1&referrer=COJUN¶ms=adref%3DGarden and DIY->Garden furniture->Garden table and chair sets&referredURL=http://www.argos.co.uk/webapp/wcs/stores/servlet/ProductDisplay%3FstoreId%3D10001%26catalogId%3D1500001501%26productId%3D1500357199%26langId%3D-1") prod_url = url_query_parameter(aff_url2, 'referredURL') self.assertEqual(prod_url, "http://www.argos.co.uk/webapp/wcs/stores/servlet/ProductDisplay?storeId=10001&catalogId=1500001501&productId=1500357199&langId=-1") # weird case aff_url1 = "http://www.tkqlhce.com/click-2590032-10294381?url=http%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FArgosCreateReferral%3FstoreId%3D10001%26langId%3D-1%26referrer%3DCOJUN%26params%3Dadref%253DGarden+and+DIY-%3EGarden+furniture-%3EChildren%26%2339%3Bs+garden+furniture%26referredURL%3Dhttp%3A%2F%2Fwww.argos.co.uk%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2FProductDisplay%253FstoreId%253D10001%2526catalogId%253D1500001501%2526productId%253D1500357023%2526langId%253D-1" aff_url2 = url_query_parameter(aff_url1, 'url') self.assertEqual(aff_url2, "http://www.argos.co.uk/webapp/wcs/stores/servlet/ArgosCreateReferral?storeId=10001&langId=-1&referrer=COJUN¶ms=adref%3DGarden and DIY->Garden furniture->Children's garden furniture&referredURL=http://www.argos.co.uk/webapp/wcs/stores/servlet/ProductDisplay%3FstoreId%3D10001%26catalogId%3D1500001501%26productId%3D1500357023%26langId%3D-1") prod_url = url_query_parameter(aff_url2, 'referredURL') # fails, prod_url is None now self.assertEqual(prod_url, "http://www.argos.co.uk/webapp/wcs/stores/servlet/ProductDisplay?storeId=10001&catalogId=1500001501&productId=1500357023&langId=-1") def test_add_or_replace_parameter(self): url = 'http://domain/test' self.assertEqual(add_or_replace_parameter(url, 'arg', 'v'), 'http://domain/test?arg=v') url = 'http://domain/test?arg1=v1&arg2=v2&arg3=v3' self.assertEqual(add_or_replace_parameter(url, 'arg4', 'v4'), 'http://domain/test?arg1=v1&arg2=v2&arg3=v3&arg4=v4') self.assertEqual(add_or_replace_parameter(url, 'arg3', 'nv3'), 'http://domain/test?arg1=v1&arg2=v2&arg3=nv3') url = 'http://domain/test?arg1=v1;arg2=v2' self.assertEqual(add_or_replace_parameter(url, 'arg1', 'v3'), 'http://domain/test?arg1=v3&arg2=v2') self.assertEqual(add_or_replace_parameter("http://domain/moreInfo.asp?prodID=", 'prodID', '20'), 'http://domain/moreInfo.asp?prodID=20') url = 'http://rmc-offers.co.uk/productlist.asp?BCat=2%2C60&CatID=60' self.assertEqual(add_or_replace_parameter(url, 'BCat', 'newvalue'), 'http://rmc-offers.co.uk/productlist.asp?BCat=newvalue&CatID=60') url = 'http://rmc-offers.co.uk/productlist.asp?BCat=2,60&CatID=60' self.assertEqual(add_or_replace_parameter(url, 'BCat', 'newvalue'), 'http://rmc-offers.co.uk/productlist.asp?BCat=newvalue&CatID=60') url = 'http://rmc-offers.co.uk/productlist.asp?' self.assertEqual(add_or_replace_parameter(url, 'BCat', 'newvalue'), 'http://rmc-offers.co.uk/productlist.asp?BCat=newvalue') url = "http://example.com/?version=1&pageurl=http%3A%2F%2Fwww.example.com%2Ftest%2F%23fragment%3Dy¶m2=value2" self.assertEqual(add_or_replace_parameter(url, 'version', '2'), 'http://example.com/?version=2&pageurl=http%3A%2F%2Fwww.example.com%2Ftest%2F%23fragment%3Dy¶m2=value2') self.assertEqual(add_or_replace_parameter(url, 'pageurl', 'test'), 'http://example.com/?version=1&pageurl=test¶m2=value2') url = 'http://domain/test?arg1=v1&arg2=v2&arg1=v3' self.assertEqual(add_or_replace_parameter(url, 'arg4', 'v4'), 'http://domain/test?arg1=v1&arg2=v2&arg1=v3&arg4=v4') self.assertEqual(add_or_replace_parameter(url, 'arg1', 'v3'), 'http://domain/test?arg1=v3&arg2=v2') def test_add_or_replace_parameters(self): url = 'http://domain/test' self.assertEqual(add_or_replace_parameters(url, {'arg': 'v'}), 'http://domain/test?arg=v') url = 'http://domain/test?arg1=v1&arg2=v2&arg3=v3' self.assertEqual(add_or_replace_parameters(url, {'arg4': 'v4'}), 'http://domain/test?arg1=v1&arg2=v2&arg3=v3&arg4=v4') self.assertEqual(add_or_replace_parameters(url, {'arg4': 'v4', 'arg3': 'v3new'}), 'http://domain/test?arg1=v1&arg2=v2&arg3=v3new&arg4=v4') url = 'http://domain/test?arg1=v1&arg2=v2&arg1=v3' self.assertEqual(add_or_replace_parameters(url, {'arg4': 'v4'}), 'http://domain/test?arg1=v1&arg2=v2&arg1=v3&arg4=v4') self.assertEqual(add_or_replace_parameters(url, {'arg1': 'v3'}), 'http://domain/test?arg1=v3&arg2=v2') def test_add_or_replace_parameters_does_not_change_input_param(self): url = 'http://domain/test?arg=original' input_param = {'arg': 'value'} new_url = add_or_replace_parameters(url, input_param) # noqa self.assertEqual(input_param, {'arg': 'value'}) def test_url_query_cleaner(self): self.assertEqual('product.html', url_query_cleaner("product.html?")) self.assertEqual('product.html', url_query_cleaner("product.html?&")) self.assertEqual('product.html?id=200', url_query_cleaner("product.html?id=200&foo=bar&name=wired", ['id'])) self.assertEqual('product.html?id=200', url_query_cleaner("product.html?&id=200&&foo=bar&name=wired", ['id'])) self.assertEqual('product.html', url_query_cleaner("product.html?foo=bar&name=wired", ['id'])) self.assertEqual('product.html?id=200&name=wired', url_query_cleaner("product.html?id=200&foo=bar&name=wired", ['id', 'name'])) self.assertEqual('product.html?id', url_query_cleaner("product.html?id&other=3&novalue=", ['id'])) # default is to remove duplicate keys self.assertEqual('product.html?d=1', url_query_cleaner("product.html?d=1&e=b&d=2&d=3&other=other", ['d'])) # unique=False disables duplicate keys filtering self.assertEqual('product.html?d=1&d=2&d=3', url_query_cleaner("product.html?d=1&e=b&d=2&d=3&other=other", ['d'], unique=False)) self.assertEqual('product.html?id=200&foo=bar', url_query_cleaner("product.html?id=200&foo=bar&name=wired#id20", ['id', 'foo'])) self.assertEqual('product.html?foo=bar&name=wired', url_query_cleaner("product.html?id=200&foo=bar&name=wired", ['id'], remove=True)) self.assertEqual('product.html?name=wired', url_query_cleaner("product.html?id=2&foo=bar&name=wired", ['id', 'foo'], remove=True)) self.assertEqual('product.html?foo=bar&name=wired', url_query_cleaner("product.html?id=2&foo=bar&name=wired", ['id', 'footo'], remove=True)) self.assertEqual('product.html', url_query_cleaner("product.html", ['id'], remove=True)) self.assertEqual('product.html', url_query_cleaner("product.html?&", ['id'], remove=True)) self.assertEqual('product.html?foo=bar', url_query_cleaner("product.html?foo=bar&name=wired", 'foo')) self.assertEqual('product.html?foobar=wired', url_query_cleaner("product.html?foo=bar&foobar=wired", 'foobar')) def test_url_query_cleaner_keep_fragments(self): self.assertEqual('product.html?id=200#foo', url_query_cleaner("product.html?id=200&foo=bar&name=wired#foo", ['id'], keep_fragments=True)) def test_path_to_file_uri(self): if os.name == 'nt': self.assertEqual(path_to_file_uri(r"C:\\windows\clock.avi"), "file:///C:/windows/clock.avi") else: self.assertEqual(path_to_file_uri("/some/path.txt"), "file:///some/path.txt") fn = "test.txt" x = path_to_file_uri(fn) self.assertTrue(x.startswith('file:///')) self.assertEqual(file_uri_to_path(x).lower(), os.path.abspath(fn).lower()) def test_file_uri_to_path(self): if os.name == 'nt': self.assertEqual(file_uri_to_path("file:///C:/windows/clock.avi"), r"C:\\windows\clock.avi") uri = "file:///C:/windows/clock.avi" uri2 = path_to_file_uri(file_uri_to_path(uri)) self.assertEqual(uri, uri2) else: self.assertEqual(file_uri_to_path("file:///path/to/test.txt"), "/path/to/test.txt") self.assertEqual(file_uri_to_path("/path/to/test.txt"), "/path/to/test.txt") uri = "file:///path/to/test.txt" uri2 = path_to_file_uri(file_uri_to_path(uri)) self.assertEqual(uri, uri2) self.assertEqual(file_uri_to_path("test.txt"), "test.txt") def test_any_to_uri(self): if os.name == 'nt': self.assertEqual(any_to_uri(r"C:\\windows\clock.avi"), "file:///C:/windows/clock.avi") else: self.assertEqual(any_to_uri("/some/path.txt"), "file:///some/path.txt") self.assertEqual(any_to_uri("file:///some/path.txt"), "file:///some/path.txt") self.assertEqual(any_to_uri("http://www.example.com/some/path.txt"), "http://www.example.com/some/path.txt") def test_urljoin_rfc_deprecated(self): jurl = urljoin_rfc("http://www.example.com/", "/test") self.assertEqual(jurl, b"http://www.example.com/test") class CanonicalizeUrlTest(unittest.TestCase): def test_canonicalize_url(self): # simplest case self.assertEqual(canonicalize_url("http://www.example.com/"), "http://www.example.com/") def test_return_str(self): assert isinstance(canonicalize_url(u"http://www.example.com"), str) assert isinstance(canonicalize_url(b"http://www.example.com"), str) def test_append_missing_path(self): self.assertEqual(canonicalize_url("http://www.example.com"), "http://www.example.com/") def test_typical_usage(self): self.assertEqual(canonicalize_url("http://www.example.com/do?a=1&b=2&c=3"), "http://www.example.com/do?a=1&b=2&c=3") self.assertEqual(canonicalize_url("http://www.example.com/do?c=1&b=2&a=3"), "http://www.example.com/do?a=3&b=2&c=1") self.assertEqual(canonicalize_url("http://www.example.com/do?&a=1"), "http://www.example.com/do?a=1") def test_port_number(self): self.assertEqual(canonicalize_url("http://www.example.com:8888/do?a=1&b=2&c=3"), "http://www.example.com:8888/do?a=1&b=2&c=3") # trailing empty ports are removed self.assertEqual(canonicalize_url("http://www.example.com:/do?a=1&b=2&c=3"), "http://www.example.com/do?a=1&b=2&c=3") def test_sorting(self): self.assertEqual(canonicalize_url("http://www.example.com/do?c=3&b=5&b=2&a=50"), "http://www.example.com/do?a=50&b=2&b=5&c=3") def test_keep_blank_values(self): self.assertEqual(canonicalize_url("http://www.example.com/do?b=&a=2", keep_blank_values=False), "http://www.example.com/do?a=2") self.assertEqual(canonicalize_url("http://www.example.com/do?b=&a=2"), "http://www.example.com/do?a=2&b=") self.assertEqual(canonicalize_url("http://www.example.com/do?b=&c&a=2", keep_blank_values=False), "http://www.example.com/do?a=2") self.assertEqual(canonicalize_url("http://www.example.com/do?b=&c&a=2"), "http://www.example.com/do?a=2&b=&c=") self.assertEqual(canonicalize_url(u'http://www.example.com/do?1750,4'), 'http://www.example.com/do?1750%2C4=') def test_spaces(self): self.assertEqual(canonicalize_url("http://www.example.com/do?q=a space&a=1"), "http://www.example.com/do?a=1&q=a+space") self.assertEqual(canonicalize_url("http://www.example.com/do?q=a+space&a=1"), "http://www.example.com/do?a=1&q=a+space") self.assertEqual(canonicalize_url("http://www.example.com/do?q=a%20space&a=1"), "http://www.example.com/do?a=1&q=a+space") def test_canonicalize_url_unicode_path(self): self.assertEqual(canonicalize_url(u"http://www.example.com/résumé"), "http://www.example.com/r%C3%A9sum%C3%A9") def test_canonicalize_url_unicode_query_string(self): # default encoding for path and query is UTF-8 self.assertEqual(canonicalize_url(u"http://www.example.com/résumé?q=résumé"), "http://www.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") # passed encoding will affect query string self.assertEqual(canonicalize_url(u"http://www.example.com/résumé?q=résumé", encoding='latin1'), "http://www.example.com/r%C3%A9sum%C3%A9?q=r%E9sum%E9") self.assertEqual(canonicalize_url(u"http://www.example.com/résumé?country=Россия", encoding='cp1251'), "http://www.example.com/r%C3%A9sum%C3%A9?country=%D0%EE%F1%F1%E8%FF") def test_canonicalize_url_unicode_query_string_wrong_encoding(self): # trying to encode with wrong encoding # fallback to UTF-8 self.assertEqual(canonicalize_url(u"http://www.example.com/résumé?currency=€", encoding='latin1'), "http://www.example.com/r%C3%A9sum%C3%A9?currency=%E2%82%AC") self.assertEqual(canonicalize_url(u"http://www.example.com/résumé?country=Россия", encoding='latin1'), "http://www.example.com/r%C3%A9sum%C3%A9?country=%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D1%8F") def test_normalize_percent_encoding_in_paths(self): self.assertEqual(canonicalize_url("http://www.example.com/r%c3%a9sum%c3%a9"), "http://www.example.com/r%C3%A9sum%C3%A9") # non-UTF8 encoded sequences: they should be kept untouched, only upper-cased # 'latin1'-encoded sequence in path self.assertEqual(canonicalize_url("http://www.example.com/a%a3do"), "http://www.example.com/a%A3do") # 'latin1'-encoded path, UTF-8 encoded query string self.assertEqual(canonicalize_url("http://www.example.com/a%a3do?q=r%c3%a9sum%c3%a9"), "http://www.example.com/a%A3do?q=r%C3%A9sum%C3%A9") # 'latin1'-encoded path and query string self.assertEqual(canonicalize_url("http://www.example.com/a%a3do?q=r%e9sum%e9"), "http://www.example.com/a%A3do?q=r%E9sum%E9") def test_normalize_percent_encoding_in_query_arguments(self): self.assertEqual(canonicalize_url("http://www.example.com/do?k=b%a3"), "http://www.example.com/do?k=b%A3") self.assertEqual(canonicalize_url("http://www.example.com/do?k=r%c3%a9sum%c3%a9"), "http://www.example.com/do?k=r%C3%A9sum%C3%A9") def test_non_ascii_percent_encoding_in_paths(self): self.assertEqual(canonicalize_url("http://www.example.com/a do?a=1"), "http://www.example.com/a%20do?a=1"), self.assertEqual(canonicalize_url("http://www.example.com/a %20do?a=1"), "http://www.example.com/a%20%20do?a=1"), self.assertEqual(canonicalize_url(u"http://www.example.com/a do£.html?a=1"), "http://www.example.com/a%20do%C2%A3.html?a=1") self.assertEqual(canonicalize_url(b"http://www.example.com/a do\xc2\xa3.html?a=1"), "http://www.example.com/a%20do%C2%A3.html?a=1") def test_non_ascii_percent_encoding_in_query_arguments(self): self.assertEqual(canonicalize_url(u"http://www.example.com/do?price=£500&a=5&z=3"), u"http://www.example.com/do?a=5&price=%C2%A3500&z=3") self.assertEqual(canonicalize_url(b"http://www.example.com/do?price=\xc2\xa3500&a=5&z=3"), "http://www.example.com/do?a=5&price=%C2%A3500&z=3") self.assertEqual(canonicalize_url(b"http://www.example.com/do?price(\xc2\xa3)=500&a=1"), "http://www.example.com/do?a=1&price%28%C2%A3%29=500") def test_urls_with_auth_and_ports(self): self.assertEqual(canonicalize_url(u"http://user:pass@www.example.com:81/do?now=1"), u"http://user:pass@www.example.com:81/do?now=1") def test_remove_fragments(self): self.assertEqual(canonicalize_url(u"http://user:pass@www.example.com/do?a=1#frag"), u"http://user:pass@www.example.com/do?a=1") self.assertEqual(canonicalize_url(u"http://user:pass@www.example.com/do?a=1#frag", keep_fragments=True), u"http://user:pass@www.example.com/do?a=1#frag") def test_dont_convert_safe_characters(self): # dont convert safe characters to percent encoding representation self.assertEqual(canonicalize_url( "http://www.simplybedrooms.com/White-Bedroom-Furniture/Bedroom-Mirror:-Josephine-Cheval-Mirror.html"), "http://www.simplybedrooms.com/White-Bedroom-Furniture/Bedroom-Mirror:-Josephine-Cheval-Mirror.html") def test_safe_characters_unicode(self): # urllib.quote uses a mapping cache of encoded characters. when parsing # an already percent-encoded url, it will fail if that url was not # percent-encoded as utf-8, that's why canonicalize_url must always # convert the urls to string. the following test asserts that # functionality. self.assertEqual(canonicalize_url(u'http://www.example.com/caf%E9-con-leche.htm'), 'http://www.example.com/caf%E9-con-leche.htm') def test_domains_are_case_insensitive(self): self.assertEqual(canonicalize_url("http://www.EXAMPLE.com/"), "http://www.example.com/") def test_canonicalize_idns(self): self.assertEqual(canonicalize_url(u'http://www.bücher.de?q=bücher'), 'http://www.xn--bcher-kva.de/?q=b%C3%BCcher') # Japanese (+ reordering query parameters) self.assertEqual(canonicalize_url(u'http://はじめよう.みんな/?query=サ&maxResults=5'), 'http://xn--p8j9a0d9c9a.xn--q9jyb4c/?maxResults=5&query=%E3%82%B5') def test_quoted_slash_and_question_sign(self): self.assertEqual(canonicalize_url("http://foo.com/AC%2FDC+rocks%3f/?yeah=1"), "http://foo.com/AC%2FDC+rocks%3F/?yeah=1") self.assertEqual(canonicalize_url("http://foo.com/AC%2FDC/"), "http://foo.com/AC%2FDC/") def test_canonicalize_urlparsed(self): # canonicalize_url() can be passed an already urlparse'd URL self.assertEqual(canonicalize_url(urlparse(u"http://www.example.com/résumé?q=résumé")), "http://www.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") self.assertEqual(canonicalize_url(urlparse('http://www.example.com/caf%e9-con-leche.htm')), 'http://www.example.com/caf%E9-con-leche.htm') self.assertEqual(canonicalize_url(urlparse("http://www.example.com/a%a3do?q=r%c3%a9sum%c3%a9")), "http://www.example.com/a%A3do?q=r%C3%A9sum%C3%A9") def test_canonicalize_parse_url(self): # parse_url() wraps urlparse and is used in link extractors self.assertEqual(canonicalize_url(parse_url(u"http://www.example.com/résumé?q=résumé")), "http://www.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") self.assertEqual(canonicalize_url(parse_url('http://www.example.com/caf%e9-con-leche.htm')), 'http://www.example.com/caf%E9-con-leche.htm') self.assertEqual(canonicalize_url(parse_url("http://www.example.com/a%a3do?q=r%c3%a9sum%c3%a9")), "http://www.example.com/a%A3do?q=r%C3%A9sum%C3%A9") def test_canonicalize_url_idempotence(self): for url, enc in [(u'http://www.bücher.de/résumé?q=résumé', 'utf8'), (u'http://www.example.com/résumé?q=résumé', 'latin1'), (u'http://www.example.com/résumé?country=Россия', 'cp1251'), (u'http://はじめよう.みんな/?query=サ&maxResults=5', 'iso2022jp')]: canonicalized = canonicalize_url(url, encoding=enc) # if we canonicalize again, we ge the same result self.assertEqual(canonicalize_url(canonicalized, encoding=enc), canonicalized) # without encoding, already canonicalized URL is canonicalized identically self.assertEqual(canonicalize_url(canonicalized), canonicalized) def test_canonicalize_url_idna_exceptions(self): # missing DNS label self.assertEqual( canonicalize_url(u"http://.example.com/résumé?q=résumé"), "http://.example.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9") # DNS label too long self.assertEqual( canonicalize_url( u"http://www.{label}.com/résumé?q=résumé".format( label=u"example"*11)), "http://www.{label}.com/r%C3%A9sum%C3%A9?q=r%C3%A9sum%C3%A9".format( label=u"example"*11)) class DataURITests(unittest.TestCase): def test_default_mediatype_charset(self): result = parse_data_uri("data:,A%20brief%20note") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.media_type_parameters, {"charset": "US-ASCII"}) self.assertEqual(result.data, b"A brief note") def test_text_uri(self): result = parse_data_uri(u"data:,A%20brief%20note") self.assertEqual(result.data, b"A brief note") def test_bytes_uri(self): result = parse_data_uri(b"data:,A%20brief%20note") self.assertEqual(result.data, b"A brief note") def test_unicode_uri(self): result = parse_data_uri(u"data:,é") self.assertEqual(result.data, u"é".encode('utf-8')) def test_default_mediatype(self): result = parse_data_uri("data:;charset=iso-8859-7,%be%d3%be") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.media_type_parameters, {"charset": "iso-8859-7"}) self.assertEqual(result.data, b"\xbe\xd3\xbe") def test_text_charset(self): result = parse_data_uri("data:text/plain;charset=iso-8859-7,%be%d3%be") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.media_type_parameters, {"charset": "iso-8859-7"}) self.assertEqual(result.data, b"\xbe\xd3\xbe") def test_mediatype_parameters(self): result = parse_data_uri('data:text/plain;' 'foo=%22foo;bar%5C%22%22;' 'charset=utf-8;' 'bar=%22foo;%5C%22foo%20;/%20,%22,' '%CE%8E%CE%A3%CE%8E') self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.media_type_parameters, {"charset": "utf-8", "foo": 'foo;bar"', "bar": 'foo;"foo ;/ ,'}) self.assertEqual(result.data, b"\xce\x8e\xce\xa3\xce\x8e") def test_base64(self): result = parse_data_uri("data:text/plain;base64," "SGVsbG8sIHdvcmxkLg%3D%3D") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.data, b"Hello, world.") def test_base64_spaces(self): result = parse_data_uri("data:text/plain;base64,SGVsb%20G8sIH%0A%20%20" "dvcm%20%20%20xk%20Lg%3D%0A%3D") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.data, b"Hello, world.") result = parse_data_uri("data:text/plain;base64,SGVsb G8sIH\n " "dvcm xk Lg%3D\n%3D") self.assertEqual(result.media_type, "text/plain") self.assertEqual(result.data, b"Hello, world.") def test_wrong_base64_param(self): with self.assertRaises(ValueError): parse_data_uri("data:text/plain;baes64,SGVsbG8sIHdvcmxkLg%3D%3D") def test_missing_comma(self): with self.assertRaises(ValueError): parse_data_uri("data:A%20brief%20note") def test_missing_scheme(self): with self.assertRaises(ValueError): parse_data_uri("text/plain,A%20brief%20note") def test_wrong_scheme(self): with self.assertRaises(ValueError): parse_data_uri("http://example.com/") def test_scheme_case_insensitive(self): result = parse_data_uri("DATA:,A%20brief%20note") self.assertEqual(result.data, b"A brief note") result = parse_data_uri("DaTa:,A%20brief%20note") self.assertEqual(result.data, b"A brief note") if __name__ == "__main__": unittest.main() w3lib-1.22.0/tox.ini0000664000372000037200000000174413657045223015006 0ustar travistravis00000000000000# Tox (http://tox.testrun.org/) is a tool for running tests # in multiple virtualenvs. This configuration file will run the # test suite on all supported python versions. To use it, "pip install tox" # and then run "tox" from this directory. [tox] envlist = py27, pypy, py35, py36, py37, py38, pypy3, docs [testenv] deps = pytest !=3.1.1, !=3.1.2 pytest-cov commands = py.test \ --doctest-modules \ --cov=w3lib --cov-report=term \ {posargs:w3lib tests} [testenv:security] deps = bandit commands = bandit -r -c .bandit.yml {posargs:w3lib} [testenv:flake8] basepython = python3 deps = {[testenv]deps} pytest-flake8 commands = pytest --flake8 [testenv:pylint] deps = {[testenv]deps} pylint commands = pylint conftest.py docs setup.py tests w3lib [docs] changedir = docs deps = -rdocs/requirements.txt [testenv:docs] changedir = {[docs]changedir} deps = {[docs]deps} commands = sphinx-build -W -b html . {envtmpdir}/html w3lib-1.22.0/w3lib/0000775000372000037200000000000013657045265014513 5ustar travistravis00000000000000w3lib-1.22.0/w3lib/__init__.py0000664000372000037200000000017713657045223016623 0ustar travistravis00000000000000__version__ = "1.22.0" version_info = tuple(int(v) if v.isdigit() else v for v in __version__.split('.')) w3lib-1.22.0/w3lib/encoding.py0000664000372000037200000002405713657045223016655 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Functions for handling encoding of web pages """ import re, codecs, encodings from sys import version_info _HEADER_ENCODING_RE = re.compile(r'charset=([\w-]+)', re.I) def http_content_type_encoding(content_type): """Extract the encoding in the content-type header >>> import w3lib.encoding >>> w3lib.encoding.http_content_type_encoding("Content-Type: text/html; charset=ISO-8859-4") 'iso8859-4' """ if content_type: match = _HEADER_ENCODING_RE.search(content_type) if match: return resolve_encoding(match.group(1)) # regexp for parsing HTTP meta tags _TEMPLATE = r'''%s\s*=\s*["']?\s*%s\s*["']?''' _SKIP_ATTRS = '''(?:\\s+ [^=<>/\\s"'\x00-\x1f\x7f]+ # Attribute name (?:\\s*=\\s* (?: # ' and " are entity encoded (', "), so no need for \', \" '[^']*' # attr in ' | "[^"]*" # attr in " | [^'"\\s]+ # attr having no ' nor " ))? )*?''' # must be used with re.VERBOSE flag _HTTPEQUIV_RE = _TEMPLATE % ('http-equiv', 'Content-Type') _CONTENT_RE = _TEMPLATE % ('content', r'(?P[^;]+);\s*charset=(?P[\w-]+)') _CONTENT2_RE = _TEMPLATE % ('charset', r'(?P[\w-]+)') _XML_ENCODING_RE = _TEMPLATE % ('encoding', r'(?P[\w-]+)') # check for meta tags, or xml decl. and stop search if a body tag is encountered _BODY_ENCODING_PATTERN = r'<\s*(?:meta%s(?:(?:\s+%s|\s+%s){2}|\s+%s)|\?xml\s[^>]+%s|body)' % ( _SKIP_ATTRS, _HTTPEQUIV_RE, _CONTENT_RE, _CONTENT2_RE, _XML_ENCODING_RE) _BODY_ENCODING_STR_RE = re.compile(_BODY_ENCODING_PATTERN, re.I | re.VERBOSE) _BODY_ENCODING_BYTES_RE = re.compile(_BODY_ENCODING_PATTERN.encode('ascii'), re.I | re.VERBOSE) def html_body_declared_encoding(html_body_str): '''Return the encoding specified in meta tags in the html body, or ``None`` if no suitable encoding was found >>> import w3lib.encoding >>> w3lib.encoding.html_body_declared_encoding( ... """ ... ... ... Some title ... ... ... ... ... ... ... """) 'utf-8' >>> ''' # html5 suggests the first 1024 bytes are sufficient, we allow for more chunk = html_body_str[:4096] if isinstance(chunk, bytes): match = _BODY_ENCODING_BYTES_RE.search(chunk) else: match = _BODY_ENCODING_STR_RE.search(chunk) if match: encoding = match.group('charset') or match.group('charset2') \ or match.group('xmlcharset') if encoding: return resolve_encoding(encoding) # Default encoding translation # this maps cannonicalized encodings to target encodings # see http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#character-encodings-0 # in addition, gb18030 supercedes gb2312 & gbk # the keys are converted using _c18n_encoding and in sorted order DEFAULT_ENCODING_TRANSLATION = { 'ascii': 'cp1252', 'big5': 'big5hkscs', 'euc_kr': 'cp949', 'gb2312': 'gb18030', 'gb_2312_80': 'gb18030', 'gbk': 'gb18030', 'iso8859_11': 'cp874', 'iso8859_9': 'cp1254', 'latin_1': 'cp1252', 'macintosh': 'mac_roman', 'shift_jis': 'cp932', 'tis_620': 'cp874', 'win_1251': 'cp1251', 'windows_31j': 'cp932', 'win_31j': 'cp932', 'windows_874': 'cp874', 'win_874': 'cp874', 'x_sjis': 'cp932', 'zh_cn': 'gb18030' } def _c18n_encoding(encoding): """Canonicalize an encoding name This performs normalization and translates aliases using python's encoding aliases """ normed = encodings.normalize_encoding(encoding).lower() return encodings.aliases.aliases.get(normed, normed) def resolve_encoding(encoding_alias): """Return the encoding that `encoding_alias` maps to, or ``None`` if the encoding cannot be interpreted >>> import w3lib.encoding >>> w3lib.encoding.resolve_encoding('latin1') 'cp1252' >>> w3lib.encoding.resolve_encoding('gb_2312-80') 'gb18030' >>> """ c18n_encoding = _c18n_encoding(encoding_alias) translated = DEFAULT_ENCODING_TRANSLATION.get(c18n_encoding, c18n_encoding) try: return codecs.lookup(translated).name except LookupError: return None _BOM_TABLE = [ (codecs.BOM_UTF32_BE, 'utf-32-be'), (codecs.BOM_UTF32_LE, 'utf-32-le'), (codecs.BOM_UTF16_BE, 'utf-16-be'), (codecs.BOM_UTF16_LE, 'utf-16-le'), (codecs.BOM_UTF8, 'utf-8') ] _FIRST_CHARS = set(c[0] for (c, _) in _BOM_TABLE) def read_bom(data): r"""Read the byte order mark in the text, if present, and return the encoding represented by the BOM and the BOM. If no BOM can be detected, ``(None, None)`` is returned. >>> import w3lib.encoding >>> w3lib.encoding.read_bom(b'\xfe\xff\x6c\x34') ('utf-16-be', '\xfe\xff') >>> w3lib.encoding.read_bom(b'\xff\xfe\x34\x6c') ('utf-16-le', '\xff\xfe') >>> w3lib.encoding.read_bom(b'\x00\x00\xfe\xff\x00\x00\x6c\x34') ('utf-32-be', '\x00\x00\xfe\xff') >>> w3lib.encoding.read_bom(b'\xff\xfe\x00\x00\x34\x6c\x00\x00') ('utf-32-le', '\xff\xfe\x00\x00') >>> w3lib.encoding.read_bom(b'\x01\x02\x03\x04') (None, None) >>> """ # common case is no BOM, so this is fast if data and data[0] in _FIRST_CHARS: for bom, encoding in _BOM_TABLE: if data.startswith(bom): return encoding, bom return None, None # Python decoder doesn't follow unicode standard when handling # bad utf-8 encoded strings. see http://bugs.python.org/issue8271 codecs.register_error('w3lib_replace', lambda exc: (u'\ufffd', exc.end)) def to_unicode(data_str, encoding): """Convert a str object to unicode using the encoding given Characters that cannot be converted will be converted to ``\\ufffd`` (the unicode replacement character). """ return data_str.decode(encoding, 'replace' if version_info[0:2] >= (3, 3) else 'w3lib_replace') def html_to_unicode(content_type_header, html_body_str, default_encoding='utf8', auto_detect_fun=None): r'''Convert raw html bytes to unicode This attempts to make a reasonable guess at the content encoding of the html body, following a similar process to a web browser. It will try in order: * http content type header * BOM (byte-order mark) * meta or xml tag declarations * auto-detection, if the `auto_detect_fun` keyword argument is not ``None`` * default encoding in keyword arg (which defaults to utf8) If an encoding other than the auto-detected or default encoding is used, overrides will be applied, converting some character encodings to more suitable alternatives. If a BOM is found matching the encoding, it will be stripped. The `auto_detect_fun` argument can be used to pass a function that will sniff the encoding of the text. This function must take the raw text as an argument and return the name of an encoding that python can process, or None. To use chardet, for example, you can define the function as:: auto_detect_fun=lambda x: chardet.detect(x).get('encoding') or to use UnicodeDammit (shipped with the BeautifulSoup library):: auto_detect_fun=lambda x: UnicodeDammit(x).originalEncoding If the locale of the website or user language preference is known, then a better default encoding can be supplied. If `content_type_header` is not present, ``None`` can be passed signifying that the header was not present. This method will not fail, if characters cannot be converted to unicode, ``\\ufffd`` (the unicode replacement character) will be inserted instead. Returns a tuple of ``(, )`` Examples: >>> import w3lib.encoding >>> w3lib.encoding.html_to_unicode(None, ... b""" ... ... ... ... Creative Commons France ... ... ...

Creative Commons est une organisation \xc3\xa0 but non lucratif ... qui a pour dessein de faciliter la diffusion et le partage des oeuvres ... tout en accompagnant les nouvelles pratiques de cr\xc3\xa9ation \xc3\xa0 l\xe2\x80\x99\xc3\xa8re numerique.

... ... """) ('utf-8', u'\n\n\n\nCreative Commons France\n\n\n

Creative Commons est une organisation \xe0 but non lucratif\nqui a pour dessein de faciliter la diffusion et le partage des oeuvres\ntout en accompagnant les nouvelles pratiques de cr\xe9ation \xe0 l\u2019\xe8re numerique.

\n\n') >>> ''' enc = http_content_type_encoding(content_type_header) bom_enc, bom = read_bom(html_body_str) if enc is not None: # remove BOM if it agrees with the encoding if enc == bom_enc: html_body_str = html_body_str[len(bom):] elif enc == 'utf-16' or enc == 'utf-32': # read endianness from BOM, or default to big endian # tools.ietf.org/html/rfc2781 section 4.3 if bom_enc is not None and bom_enc.startswith(enc): enc = bom_enc html_body_str = html_body_str[len(bom):] else: enc += '-be' return enc, to_unicode(html_body_str, enc) if bom_enc is not None: return bom_enc, to_unicode(html_body_str[len(bom):], bom_enc) enc = html_body_declared_encoding(html_body_str) if enc is None and (auto_detect_fun is not None): enc = auto_detect_fun(html_body_str) if enc is None: enc = default_encoding return enc, to_unicode(html_body_str, enc) w3lib-1.22.0/w3lib/form.py0000664000372000037200000000612313657045223016024 0ustar travistravis00000000000000import warnings import six if six.PY2: from cStringIO import StringIO as BytesIO else: from io import BytesIO from w3lib.util import unicode_to_str def encode_multipart(data): r""" .. warning:: This function is deprecated and will be removed in future. Please use ``urllib3.filepost.encode_multipart_formdata`` instead. Encode the given data to be used in a multipart HTTP POST. `data` is a dictionary where keys are the field name, and values are either strings or tuples as `(filename, content)` for file uploads. This code is based on :class:`distutils.command.upload`. Returns a `(body, boundary)` tuple where `body` is binary body value, and `boundary` is the boundary used (as native string). >>> import w3lib.form >>> w3lib.form.encode_multipart({'key': 'value'}) ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key"\r\n\r\nvalue\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') >>> w3lib.form.encode_multipart({'key1': 'value1', 'key2': 'value2'}) # doctest: +SKIP ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key2"\r\n\r\nvalue2\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="key1"\r\n\r\nvalue1\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') >>> w3lib.form.encode_multipart({'somekey': ('path/to/filename', b'\xa1\xa2\xa3\xa4\r\n\r')}) ('\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254\r\nContent-Disposition: form-data; name="somekey"; filename="path/to/filename"\r\n\r\n\xa1\xa2\xa3\xa4\r\n\r\r\n----------------GHSKFJDLGDS7543FJKLFHRE75642756743254--\r\n', '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254') >>> """ warnings.warn( "`w3lib.form.encode_multipart` function is deprecated and " "will be removed in future releases. Please use " "`urllib3.filepost.encode_multipart_formdata` instead.", DeprecationWarning ) # Build up the MIME payload for the POST data boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' sep_boundary = b'\r\n--' + boundary.encode('ascii') end_boundary = sep_boundary + b'--' body = BytesIO() for key, value in data.items(): title = u'\r\nContent-Disposition: form-data; name="%s"' % key # handle multiple entries for the same name if type(value) != type([]): value = [value] for value in value: if type(value) is tuple: title += u'; filename="%s"' % value[0] value = value[1] else: value = unicode_to_str(value) # in distutils: str(value).encode('utf-8') body.write(sep_boundary) body.write(title.encode('utf-8')) body.write(b"\r\n\r\n") body.write(value) body.write(end_boundary) body.write(b"\r\n") return body.getvalue(), boundary w3lib-1.22.0/w3lib/html.py0000664000372000037200000002627413657045223016036 0ustar travistravis00000000000000# -*- coding: utf-8 -*- """ Functions for dealing with markup text """ import warnings import re import six from six import moves from w3lib.util import to_bytes, to_unicode from w3lib.url import safe_url_string _ent_re = re.compile(r'&((?P[a-z\d]+)|#(?P\d+)|#x(?P[a-f\d]+))(?P;?)', re.IGNORECASE) _tag_re = re.compile(r'<[a-zA-Z\/!].*?>', re.DOTALL) _baseurl_re = re.compile(six.u(r']*href\s*=\s*[\"\']\s*([^\"\'\s]+)\s*[\"\']'), re.I) _meta_refresh_re = re.compile(six.u(r']*http-equiv[^>]*refresh[^>]*content\s*=\s*(?P["\'])(?P(\d*\.)?\d+)\s*;\s*url=\s*(?P.*?)(?P=quote)'), re.DOTALL | re.IGNORECASE) _cdata_re = re.compile(r'((?P.*?)(?P\]\]>))', re.DOTALL) HTML5_WHITESPACE = ' \t\n\r\x0c' def remove_entities(text, keep=(), remove_illegal=True, encoding='utf-8'): r""" .. warning:: This function is deprecated and will be removed in future. Please use :func:`replace_entities` instead. """ warnings.warn( "`w3lib.html.remove_entities` function is deprecated and " "will be removed in future releases. Please use " "`w3lib.html.replace_entities` instead.", DeprecationWarning ) return replace_entities(text, keep, remove_illegal, encoding) def replace_entities(text, keep=(), remove_illegal=True, encoding='utf-8'): u"""Remove entities from the given `text` by converting them to their corresponding unicode character. `text` can be a unicode string or a byte string encoded in the given `encoding` (which defaults to 'utf-8'). If `keep` is passed (with a list of entity names) those entities will be kept (they won't be removed). It supports both numeric entities (``&#nnnn;`` and ``&#hhhh;``) and named entities (such as `` `` or ``>``). If `remove_illegal` is ``True``, entities that can't be converted are removed. If `remove_illegal` is ``False``, entities that can't be converted are kept "as is". For more information see the tests. Always returns a unicode string (with the entities removed). >>> import w3lib.html >>> w3lib.html.replace_entities(b'Price: £100') u'Price: \\xa3100' >>> print(w3lib.html.replace_entities(b'Price: £100')) Price: £100 >>> """ def convert_entity(m): groups = m.groupdict() if groups.get('dec'): number = int(groups['dec'], 10) elif groups.get('hex'): number = int(groups['hex'], 16) elif groups.get('named'): entity_name = groups['named'] if entity_name.lower() in keep: return m.group(0) else: number = (moves.html_entities.name2codepoint.get(entity_name) or moves.html_entities.name2codepoint.get(entity_name.lower())) if number is not None: # Numeric character references in the 80-9F range are typically # interpreted by browsers as representing the characters mapped # to bytes 80-9F in the Windows-1252 encoding. For more info # see: http://en.wikipedia.org/wiki/Character_encodings_in_HTML try: if 0x80 <= number <= 0x9f: return six.int2byte(number).decode('cp1252') else: return six.unichr(number) except ValueError: pass return u'' if remove_illegal and groups.get('semicolon') else m.group(0) return _ent_re.sub(convert_entity, to_unicode(text, encoding)) def has_entities(text, encoding=None): return bool(_ent_re.search(to_unicode(text, encoding))) def replace_tags(text, token='', encoding=None): """Replace all markup tags found in the given `text` by the given token. By default `token` is an empty string so it just removes all tags. `text` can be a unicode string or a regular string encoded as `encoding` (or ``'utf-8'`` if `encoding` is not given.) Always returns a unicode string. Examples: >>> import w3lib.html >>> w3lib.html.replace_tags(u'This text contains some tag') u'This text contains some tag' >>> w3lib.html.replace_tags('

Je ne parle pas fran\\xe7ais

', ' -- ', 'latin-1') u' -- Je ne parle pas -- fran\\xe7ais -- -- ' >>> """ return _tag_re.sub(token, to_unicode(text, encoding)) _REMOVECOMMENTS_RE = re.compile(u'|$)', re.DOTALL) def remove_comments(text, encoding=None): """ Remove HTML Comments. >>> import w3lib.html >>> w3lib.html.remove_comments(b"test whatever") u'test whatever' >>> """ text = to_unicode(text, encoding) return _REMOVECOMMENTS_RE.sub(u'', text) def remove_tags(text, which_ones=(), keep=(), encoding=None): """ Remove HTML Tags only. `which_ones` and `keep` are both tuples, there are four cases: ============== ============= ========================================== ``which_ones`` ``keep`` what it does ============== ============= ========================================== **not empty** empty remove all tags in ``which_ones`` empty **not empty** remove all tags except the ones in ``keep`` empty empty remove all tags **not empty** **not empty** not allowed ============== ============= ========================================== Remove all tags: >>> import w3lib.html >>> doc = '

This is a link: example

' >>> w3lib.html.remove_tags(doc) u'This is a link: example' >>> Keep only some tags: >>> w3lib.html.remove_tags(doc, keep=('div',)) u'
This is a link: example
' >>> Remove only specific tags: >>> w3lib.html.remove_tags(doc, which_ones=('a','b')) u'

This is a link: example

' >>> You can't remove some and keep some: >>> w3lib.html.remove_tags(doc, which_ones=('a',), keep=('p',)) Traceback (most recent call last): ... ValueError: Cannot use both which_ones and keep >>> """ if which_ones and keep: raise ValueError('Cannot use both which_ones and keep') which_ones = {tag.lower() for tag in which_ones} keep = {tag.lower() for tag in keep} def will_remove(tag): tag = tag.lower() if which_ones: return tag in which_ones else: return tag not in keep def remove_tag(m): tag = m.group(1) return u'' if will_remove(tag) else m.group(0) regex = '/]+).*?>' retags = re.compile(regex, re.DOTALL | re.IGNORECASE) return retags.sub(remove_tag, to_unicode(text, encoding)) def remove_tags_with_content(text, which_ones=(), encoding=None): """Remove tags and their content. `which_ones` is a tuple of which tags to remove including their content. If is empty, returns the string unmodified. >>> import w3lib.html >>> doc = '

This is a link: example

' >>> w3lib.html.remove_tags_with_content(doc, which_ones=('b',)) u'' >>> """ text = to_unicode(text, encoding) if which_ones: tags = '|'.join([r'<%s\b.*?|<%s\s*/>' % (tag, tag, tag) for tag in which_ones]) retags = re.compile(tags, re.DOTALL | re.IGNORECASE) text = retags.sub(u'', text) return text def replace_escape_chars(text, which_ones=('\n', '\t', '\r'), replace_by=u'', \ encoding=None): """Remove escape characters. `which_ones` is a tuple of which escape characters we want to remove. By default removes ``\\n``, ``\\t``, ``\\r``. `replace_by` is the string to replace the escape characters by. It defaults to ``''``, meaning the escape characters are removed. """ text = to_unicode(text, encoding) for ec in which_ones: text = text.replace(ec, to_unicode(replace_by, encoding)) return text def unquote_markup(text, keep=(), remove_illegal=True, encoding=None): """ This function receives markup as a text (always a unicode string or a UTF-8 encoded string) and does the following: 1. removes entities (except the ones in `keep`) from any part of it that is not inside a CDATA 2. searches for CDATAs and extracts their text (if any) without modifying it. 3. removes the found CDATAs """ def _get_fragments(txt, pattern): offset = 0 for match in pattern.finditer(txt): match_s, match_e = match.span(1) yield txt[offset:match_s] yield match offset = match_e yield txt[offset:] text = to_unicode(text, encoding) ret_text = u'' for fragment in _get_fragments(text, _cdata_re): if isinstance(fragment, six.string_types): # it's not a CDATA (so we try to remove its entities) ret_text += replace_entities(fragment, keep=keep, remove_illegal=remove_illegal) else: # it's a CDATA (so we just extract its content) ret_text += fragment.group('cdata_d') return ret_text def get_base_url(text, baseurl='', encoding='utf-8'): """Return the base url if declared in the given HTML `text`, relative to the given base url. If no base url is found, the given `baseurl` is returned. """ text = to_unicode(text, encoding) m = _baseurl_re.search(text) if m: return moves.urllib.parse.urljoin( safe_url_string(baseurl), safe_url_string(m.group(1), encoding=encoding) ) else: return safe_url_string(baseurl) def get_meta_refresh(text, baseurl='', encoding='utf-8', ignore_tags=('script', 'noscript')): """Return the http-equiv parameter of the HTML meta element from the given HTML text and return a tuple ``(interval, url)`` where interval is an integer containing the delay in seconds (or zero if not present) and url is a string with the absolute url to redirect. If no meta redirect is found, ``(None, None)`` is returned. """ if six.PY2: baseurl = to_bytes(baseurl, encoding) try: text = to_unicode(text, encoding) except UnicodeDecodeError: print(text) raise text = remove_tags_with_content(text, ignore_tags) text = remove_comments(replace_entities(text)) m = _meta_refresh_re.search(text) if m: interval = float(m.group('int')) url = safe_url_string(m.group('url').strip(' "\''), encoding) url = moves.urllib.parse.urljoin(baseurl, url) return interval, url else: return None, None def strip_html5_whitespace(text): r""" Strip all leading and trailing space characters (as defined in https://www.w3.org/TR/html5/infrastructure.html#space-character). Such stripping is useful e.g. for processing HTML element attributes which contain URLs, like ``href``, ``src`` or form ``action`` - HTML5 standard defines them as "valid URL potentially surrounded by spaces" or "valid non-empty URL potentially surrounded by spaces". >>> strip_html5_whitespace(' hello\n') 'hello' """ return text.strip(HTML5_WHITESPACE) w3lib-1.22.0/w3lib/http.py0000664000372000037200000000527113657045223016043 0ustar travistravis00000000000000from base64 import urlsafe_b64encode def headers_raw_to_dict(headers_raw): r""" Convert raw headers (single multi-line bytestring) to a dictionary. For example: >>> import w3lib.http >>> w3lib.http.headers_raw_to_dict(b"Content-type: text/html\n\rAccept: gzip\n\n") # doctest: +SKIP {'Content-type': ['text/html'], 'Accept': ['gzip']} Incorrect input: >>> w3lib.http.headers_raw_to_dict(b"Content-typt gzip\n\n") {} >>> Argument is ``None`` (return ``None``): >>> w3lib.http.headers_raw_to_dict(None) >>> """ if headers_raw is None: return None headers = headers_raw.splitlines() headers_tuples = [header.split(b':', 1) for header in headers] result_dict = {} for header_item in headers_tuples: if not len(header_item) == 2: continue item_key = header_item[0].strip() item_value = header_item[1].strip() if item_key in result_dict: result_dict[item_key].append(item_value) else: result_dict[item_key] = [item_value] return result_dict def headers_dict_to_raw(headers_dict): r""" Returns a raw HTTP headers representation of headers For example: >>> import w3lib.http >>> w3lib.http.headers_dict_to_raw({b'Content-type': b'text/html', b'Accept': b'gzip'}) # doctest: +SKIP 'Content-type: text/html\\r\\nAccept: gzip' >>> Note that keys and values must be bytes. Argument is ``None`` (returns ``None``): >>> w3lib.http.headers_dict_to_raw(None) >>> """ if headers_dict is None: return None raw_lines = [] for key, value in headers_dict.items(): if isinstance(value, bytes): raw_lines.append(b": ".join([key, value])) elif isinstance(value, (list, tuple)): for v in value: raw_lines.append(b": ".join([key, v])) return b'\r\n'.join(raw_lines) def basic_auth_header(username, password, encoding='ISO-8859-1'): """ Return an `Authorization` header field value for `HTTP Basic Access Authentication (RFC 2617)`_ >>> import w3lib.http >>> w3lib.http.basic_auth_header('someuser', 'somepass') 'Basic c29tZXVzZXI6c29tZXBhc3M=' .. _HTTP Basic Access Authentication (RFC 2617): http://www.ietf.org/rfc/rfc2617.txt """ auth = "%s:%s" % (username, password) if not isinstance(auth, bytes): # XXX: RFC 2617 doesn't define encoding, but ISO-8859-1 # seems to be the most widely used encoding here. See also: # http://greenbytes.de/tech/webdav/draft-ietf-httpauth-basicauth-enc-latest.html auth = auth.encode(encoding) return b'Basic ' + urlsafe_b64encode(auth) w3lib-1.22.0/w3lib/url.py0000664000372000037200000005412213657045223015665 0ustar travistravis00000000000000""" This module contains general purpose URL functions not found in the standard library. """ import base64 import codecs import os import re import posixpath import warnings import string from collections import namedtuple import six from six.moves.urllib.parse import (urljoin, urlsplit, urlunsplit, urldefrag, urlencode, urlparse, quote, parse_qs, parse_qsl, ParseResult, unquote, urlunparse) from six.moves.urllib.request import pathname2url, url2pathname from w3lib.util import to_bytes, to_native_str, to_unicode # error handling function for bytes-to-Unicode decoding errors with URLs def _quote_byte(error): return (to_unicode(quote(error.object[error.start:error.end])), error.end) codecs.register_error('percentencode', _quote_byte) # constants from RFC 3986, Section 2.2 and 2.3 RFC3986_GEN_DELIMS = b':/?#[]@' RFC3986_SUB_DELIMS = b"!$&'()*+,;=" RFC3986_RESERVED = RFC3986_GEN_DELIMS + RFC3986_SUB_DELIMS RFC3986_UNRESERVED = (string.ascii_letters + string.digits + "-._~").encode('ascii') EXTRA_SAFE_CHARS = b'|' # see https://github.com/scrapy/w3lib/pull/25 _safe_chars = RFC3986_RESERVED + RFC3986_UNRESERVED + EXTRA_SAFE_CHARS + b'%' _ascii_tab_newline_re = re.compile(r'[\t\n\r]') # see https://infra.spec.whatwg.org/#ascii-tab-or-newline def safe_url_string(url, encoding='utf8', path_encoding='utf8', quote_path=True): """Convert the given URL into a legal URL by escaping unsafe characters according to RFC-3986. Also, ASCII tabs and newlines are removed as per https://url.spec.whatwg.org/#url-parsing. If a bytes URL is given, it is first converted to `str` using the given encoding (which defaults to 'utf-8'). If quote_path is True (default), path_encoding ('utf-8' by default) is used to encode URL path component which is then quoted. Otherwise, if quote_path is False, path component is not encoded or quoted. Given encoding is used for query string or form data. When passing an encoding, you should use the encoding of the original page (the page from which the URL was extracted from). Calling this function on an already "safe" URL will return the URL unmodified. Always returns a native `str` (bytes in Python2, unicode in Python3). """ # Python3's urlsplit() chokes on bytes input with non-ASCII chars, # so let's decode (to Unicode) using page encoding: # - it is assumed that a raw bytes input comes from a document # encoded with the supplied encoding (or UTF8 by default) # - if the supplied (or default) encoding chokes, # percent-encode offending bytes decoded = to_unicode(url, encoding=encoding, errors='percentencode') parts = urlsplit(_ascii_tab_newline_re.sub('', decoded)) # IDNA encoding can fail for too long labels (>63 characters) # or missing labels (e.g. http://.example.com) try: netloc = parts.netloc.encode('idna') except UnicodeError: netloc = parts.netloc # default encoding for path component SHOULD be UTF-8 if quote_path: path = quote(to_bytes(parts.path, path_encoding), _safe_chars) else: path = to_native_str(parts.path) # quote() in Python2 return type follows input type; # quote() in Python3 always returns Unicode (native str) return urlunsplit(( to_native_str(parts.scheme), to_native_str(netloc).rstrip(':'), path, # encoding of query and fragment follows page encoding # or form-charset (if known and passed) quote(to_bytes(parts.query, encoding), _safe_chars), quote(to_bytes(parts.fragment, encoding), _safe_chars), )) _parent_dirs = re.compile(r'/?(\.\./)+') def safe_download_url(url, encoding='utf8', path_encoding='utf8'): """ Make a url for download. This will call safe_url_string and then strip the fragment, if one exists. The path will be normalised. If the path is outside the document root, it will be changed to be within the document root. """ safe_url = safe_url_string(url, encoding, path_encoding) scheme, netloc, path, query, _ = urlsplit(safe_url) if path: path = _parent_dirs.sub('', posixpath.normpath(path)) if safe_url.endswith('/') and not path.endswith('/'): path += '/' else: path = '/' return urlunsplit((scheme, netloc, path, query, '')) def is_url(text): return text.partition("://")[0] in ('file', 'http', 'https') def url_query_parameter(url, parameter, default=None, keep_blank_values=0): """Return the value of a url parameter, given the url and parameter name General case: >>> import w3lib.url >>> w3lib.url.url_query_parameter("product.html?id=200&foo=bar", "id") '200' >>> Return a default value if the parameter is not found: >>> w3lib.url.url_query_parameter("product.html?id=200&foo=bar", "notthere", "mydefault") 'mydefault' >>> Returns None if `keep_blank_values` not set or 0 (default): >>> w3lib.url.url_query_parameter("product.html?id=", "id") >>> Returns an empty string if `keep_blank_values` set to 1: >>> w3lib.url.url_query_parameter("product.html?id=", "id", keep_blank_values=1) '' >>> """ queryparams = parse_qs( urlsplit(str(url))[3], keep_blank_values=keep_blank_values ) return queryparams.get(parameter, [default])[0] def url_query_cleaner(url, parameterlist=(), sep='&', kvsep='=', remove=False, unique=True, keep_fragments=False): """Clean URL arguments leaving only those passed in the parameterlist keeping order >>> import w3lib.url >>> w3lib.url.url_query_cleaner("product.html?id=200&foo=bar&name=wired", ('id',)) 'product.html?id=200' >>> w3lib.url.url_query_cleaner("product.html?id=200&foo=bar&name=wired", ['id', 'name']) 'product.html?id=200&name=wired' >>> If `unique` is ``False``, do not remove duplicated keys >>> w3lib.url.url_query_cleaner("product.html?d=1&e=b&d=2&d=3&other=other", ['d'], unique=False) 'product.html?d=1&d=2&d=3' >>> If `remove` is ``True``, leave only those **not in parameterlist**. >>> w3lib.url.url_query_cleaner("product.html?id=200&foo=bar&name=wired", ['id'], remove=True) 'product.html?foo=bar&name=wired' >>> w3lib.url.url_query_cleaner("product.html?id=2&foo=bar&name=wired", ['id', 'foo'], remove=True) 'product.html?name=wired' >>> By default, URL fragments are removed. If you need to preserve fragments, pass the ``keep_fragments`` argument as ``True``. >>> w3lib.url.url_query_cleaner('http://domain.tld/?bla=123#123123', ['bla'], remove=True, keep_fragments=True) 'http://domain.tld/#123123' """ if isinstance(parameterlist, (six.text_type, bytes)): parameterlist = [parameterlist] url, fragment = urldefrag(url) base, _, query = url.partition('?') seen = set() querylist = [] for ksv in query.split(sep): if not ksv: continue k, _, _ = ksv.partition(kvsep) if unique and k in seen: continue elif remove and k in parameterlist: continue elif not remove and k not in parameterlist: continue else: querylist.append(ksv) seen.add(k) url = '?'.join([base, sep.join(querylist)]) if querylist else base if keep_fragments: url += '#' + fragment return url def _add_or_replace_parameters(url, params): parsed = urlsplit(url) current_args = parse_qsl(parsed.query, keep_blank_values=True) new_args = [] seen_params = set() for name, value in current_args: if name not in params: new_args.append((name, value)) elif name not in seen_params: new_args.append((name, params[name])) seen_params.add(name) not_modified_args = [(name, value) for name, value in params.items() if name not in seen_params] new_args += not_modified_args query = urlencode(new_args) return urlunsplit(parsed._replace(query=query)) def add_or_replace_parameter(url, name, new_value): """Add or remove a parameter to a given url >>> import w3lib.url >>> w3lib.url.add_or_replace_parameter('http://www.example.com/index.php', 'arg', 'v') 'http://www.example.com/index.php?arg=v' >>> w3lib.url.add_or_replace_parameter('http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3', 'arg4', 'v4') 'http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3&arg4=v4' >>> w3lib.url.add_or_replace_parameter('http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3', 'arg3', 'v3new') 'http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3new' >>> """ return _add_or_replace_parameters(url, {name: new_value}) def add_or_replace_parameters(url, new_parameters): """Add or remove a parameters to a given url >>> import w3lib.url >>> w3lib.url.add_or_replace_parameters('http://www.example.com/index.php', {'arg': 'v'}) 'http://www.example.com/index.php?arg=v' >>> args = {'arg4': 'v4', 'arg3': 'v3new'} >>> w3lib.url.add_or_replace_parameters('http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3', args) 'http://www.example.com/index.php?arg1=v1&arg2=v2&arg3=v3new&arg4=v4' >>> """ return _add_or_replace_parameters(url, new_parameters) def path_to_file_uri(path): """Convert local filesystem path to legal File URIs as described in: http://en.wikipedia.org/wiki/File_URI_scheme """ x = pathname2url(os.path.abspath(path)) if os.name == 'nt': x = x.replace('|', ':') # http://bugs.python.org/issue5861 return 'file:///%s' % x.lstrip('/') def file_uri_to_path(uri): """Convert File URI to local filesystem path according to: http://en.wikipedia.org/wiki/File_URI_scheme """ uri_path = urlparse(uri).path return url2pathname(uri_path) def any_to_uri(uri_or_path): """If given a path name, return its File URI, otherwise return it unmodified """ if os.path.splitdrive(uri_or_path)[0]: return path_to_file_uri(uri_or_path) u = urlparse(uri_or_path) return uri_or_path if u.scheme else path_to_file_uri(uri_or_path) # ASCII characters. _char = set(map(chr, range(127))) # RFC 2045 token. _token = r'[{}]+'.format(re.escape(''.join(_char - # Control characters. set(map(chr, range(0, 32))) - # tspecials and space. set('()<>@,;:\\"/[]?= ')))) # RFC 822 quoted-string, without surrounding quotation marks. _quoted_string = r'(?:[{}]|(?:\\[{}]))*'.format( re.escape(''.join(_char - {'"', '\\', '\r'})), re.escape(''.join(_char)) ) # Encode the regular expression strings to make them into bytes, as Python 3 # bytes have no format() method, but bytes must be passed to re.compile() in # order to make a pattern object that can be used to match on bytes. # RFC 2397 mediatype. _mediatype_pattern = re.compile( r'{token}/{token}'.format(token=_token).encode() ) _mediatype_parameter_pattern = re.compile( r';({token})=(?:({token})|"({quoted})")'.format(token=_token, quoted=_quoted_string ).encode() ) _ParseDataURIResult = namedtuple("ParseDataURIResult", "media_type media_type_parameters data") def parse_data_uri(uri): """ Parse a data: URI, returning a 3-tuple of media type, dictionary of media type parameters, and data. """ if not isinstance(uri, bytes): uri = safe_url_string(uri).encode('ascii') try: scheme, uri = uri.split(b':', 1) except ValueError: raise ValueError("invalid URI") if scheme.lower() != b'data': raise ValueError("not a data URI") # RFC 3986 section 2.1 allows percent encoding to escape characters that # would be interpreted as delimiters, implying that actual delimiters # should not be percent-encoded. # Decoding before parsing will allow malformed URIs with percent-encoded # delimiters, but it makes parsing easier and should not affect # well-formed URIs, as the delimiters used in this URI scheme are not # allowed, percent-encoded or not, in tokens. if six.PY2: uri = unquote(uri) else: uri = unquote_to_bytes(uri) media_type = "text/plain" media_type_params = {} m = _mediatype_pattern.match(uri) if m: media_type = m.group().decode() uri = uri[m.end():] else: media_type_params['charset'] = "US-ASCII" while True: m = _mediatype_parameter_pattern.match(uri) if m: attribute, value, value_quoted = m.groups() if value_quoted: value = re.sub(br'\\(.)', r'\1', value_quoted) media_type_params[attribute.decode()] = value.decode() uri = uri[m.end():] else: break try: is_base64, data = uri.split(b',', 1) except ValueError: raise ValueError("invalid data URI") if is_base64: if is_base64 != b";base64": raise ValueError("invalid data URI") data = base64.b64decode(data) return _ParseDataURIResult(media_type, media_type_params, data) __all__ = ["add_or_replace_parameter", "add_or_replace_parameters", "any_to_uri", "canonicalize_url", "file_uri_to_path", "is_url", "parse_data_uri", "path_to_file_uri", "safe_download_url", "safe_url_string", "url_query_cleaner", "url_query_parameter", # this last one is deprecated ; include it to be on the safe side "urljoin_rfc"] def _safe_ParseResult(parts, encoding='utf8', path_encoding='utf8'): # IDNA encoding can fail for too long labels (>63 characters) # or missing labels (e.g. http://.example.com) try: netloc = parts.netloc.encode('idna') except UnicodeError: netloc = parts.netloc return ( to_native_str(parts.scheme), to_native_str(netloc), # default encoding for path component SHOULD be UTF-8 quote(to_bytes(parts.path, path_encoding), _safe_chars), quote(to_bytes(parts.params, path_encoding), _safe_chars), # encoding of query and fragment follows page encoding # or form-charset (if known and passed) quote(to_bytes(parts.query, encoding), _safe_chars), quote(to_bytes(parts.fragment, encoding), _safe_chars) ) def canonicalize_url(url, keep_blank_values=True, keep_fragments=False, encoding=None): r"""Canonicalize the given url by applying the following procedures: - sort query arguments, first by key, then by value - percent encode paths ; non-ASCII characters are percent-encoded using UTF-8 (RFC-3986) - percent encode query arguments ; non-ASCII characters are percent-encoded using passed `encoding` (UTF-8 by default) - normalize all spaces (in query arguments) '+' (plus symbol) - normalize percent encodings case (%2f -> %2F) - remove query arguments with blank values (unless `keep_blank_values` is True) - remove fragments (unless `keep_fragments` is True) The url passed can be bytes or unicode, while the url returned is always a native str (bytes in Python 2, unicode in Python 3). >>> import w3lib.url >>> >>> # sorting query arguments >>> w3lib.url.canonicalize_url('http://www.example.com/do?c=3&b=5&b=2&a=50') 'http://www.example.com/do?a=50&b=2&b=5&c=3' >>> >>> # UTF-8 conversion + percent-encoding of non-ASCII characters >>> w3lib.url.canonicalize_url(u'http://www.example.com/r\u00e9sum\u00e9') 'http://www.example.com/r%C3%A9sum%C3%A9' >>> For more examples, see the tests in `tests/test_url.py`. """ # If supplied `encoding` is not compatible with all characters in `url`, # fallback to UTF-8 as safety net. # UTF-8 can handle all Unicode characters, # so we should be covered regarding URL normalization, # if not for proper URL expected by remote website. try: scheme, netloc, path, params, query, fragment = _safe_ParseResult( parse_url(url), encoding=encoding) except UnicodeEncodeError as e: scheme, netloc, path, params, query, fragment = _safe_ParseResult( parse_url(url), encoding='utf8') # 1. decode query-string as UTF-8 (or keep raw bytes), # sort values, # and percent-encode them back if six.PY2: keyvals = parse_qsl(query, keep_blank_values) else: # Python3's urllib.parse.parse_qsl does not work as wanted # for percent-encoded characters that do not match passed encoding, # they get lost. # # e.g., 'q=b%a3' becomes [('q', 'b\ufffd')] # (ie. with 'REPLACEMENT CHARACTER' (U+FFFD), # instead of \xa3 that you get with Python2's parse_qsl) # # what we want here is to keep raw bytes, and percent encode them # so as to preserve whatever encoding what originally used. # # See https://tools.ietf.org/html/rfc3987#section-6.4: # # For example, it is possible to have a URI reference of # "http://www.example.org/r%E9sum%E9.xml#r%C3%A9sum%C3%A9", where the # document name is encoded in iso-8859-1 based on server settings, but # where the fragment identifier is encoded in UTF-8 according to # [XPointer]. The IRI corresponding to the above URI would be (in XML # notation) # "http://www.example.org/r%E9sum%E9.xml#résumé". # Similar considerations apply to query parts. The functionality of # IRIs (namely, to be able to include non-ASCII characters) can only be # used if the query part is encoded in UTF-8. keyvals = parse_qsl_to_bytes(query, keep_blank_values) keyvals.sort() query = urlencode(keyvals) # 2. decode percent-encoded sequences in path as UTF-8 (or keep raw bytes) # and percent-encode path again (this normalizes to upper-case %XX) uqp = _unquotepath(path) path = quote(uqp, _safe_chars) or '/' fragment = '' if not keep_fragments else fragment # every part should be safe already return urlunparse((scheme, netloc.lower().rstrip(':'), path, params, query, fragment)) def _unquotepath(path): for reserved in ('2f', '2F', '3f', '3F'): path = path.replace('%' + reserved, '%25' + reserved.upper()) if six.PY2: # in Python 2, '%a3' becomes '\xa3', which is what we want return unquote(path) else: # in Python 3, # standard lib's unquote() does not work for non-UTF-8 # percent-escaped characters, they get lost. # e.g., '%a3' becomes 'REPLACEMENT CHARACTER' (U+FFFD) # # unquote_to_bytes() returns raw bytes instead return unquote_to_bytes(path) def parse_url(url, encoding=None): """Return urlparsed url from the given argument (which could be an already parsed url) """ if isinstance(url, ParseResult): return url return urlparse(to_unicode(url, encoding)) if not six.PY2: from urllib.parse import _coerce_args, unquote_to_bytes def parse_qsl_to_bytes(qs, keep_blank_values=False): """Parse a query given as a string argument. Data are returned as a list of name, value pairs as bytes. Arguments: qs: percent-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included. """ # This code is the same as Python3's parse_qsl() # (at https://hg.python.org/cpython/rev/c38ac7ab8d9a) # except for the unquote(s, encoding, errors) calls replaced # with unquote_to_bytes(s) qs, _coerce_result = _coerce_args(qs) pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] r = [] for name_value in pairs: if not name_value: continue nv = name_value.split('=', 1) if len(nv) != 2: # Handle case of a control-name with no equal sign if keep_blank_values: nv.append('') else: continue if len(nv[1]) or keep_blank_values: name = nv[0].replace('+', ' ') name = unquote_to_bytes(name) name = _coerce_result(name) value = nv[1].replace('+', ' ') value = unquote_to_bytes(value) value = _coerce_result(value) r.append((name, value)) return r def urljoin_rfc(base, ref, encoding='utf-8'): r""" .. warning:: This function is deprecated and will be removed in future. It is not supported with Python 3. Please use ``urlparse.urljoin`` instead. Same as urlparse.urljoin but supports unicode values in base and ref parameters (in which case they will be converted to str using the given encoding). Always returns a str. >>> import w3lib.url >>> w3lib.url.urljoin_rfc('http://www.example.com/path/index.html', u'/otherpath/index2.html') 'http://www.example.com/otherpath/index2.html' >>> >>> # Note: the following does not work in Python 3 >>> w3lib.url.urljoin_rfc(b'http://www.example.com/path/index.html', u'fran\u00e7ais/d\u00e9part.htm') # doctest: +SKIP 'http://www.example.com/path/fran\xc3\xa7ais/d\xc3\xa9part.htm' >>> """ warnings.warn("w3lib.url.urljoin_rfc is deprecated, use urlparse.urljoin instead", DeprecationWarning) str_base = to_bytes(base, encoding) str_ref = to_bytes(ref, encoding) return urljoin(str_base, str_ref) w3lib-1.22.0/w3lib/util.py0000664000372000037200000000330713657045223016037 0ustar travistravis00000000000000import six def str_to_unicode(text, encoding=None, errors='strict'): if encoding is None: encoding = 'utf-8' if isinstance(text, bytes): return text.decode(encoding, errors) return text def unicode_to_str(text, encoding=None, errors='strict'): if encoding is None: encoding = 'utf-8' if isinstance(text, six.text_type): return text.encode(encoding, errors) return text def to_unicode(text, encoding=None, errors='strict'): """Return the unicode representation of a bytes object `text`. If `text` is already an unicode object, return it as-is.""" if isinstance(text, six.text_type): return text if not isinstance(text, (bytes, six.text_type)): raise TypeError('to_unicode must receive a bytes, str or unicode ' 'object, got %s' % type(text).__name__) if encoding is None: encoding = 'utf-8' return text.decode(encoding, errors) def to_bytes(text, encoding=None, errors='strict'): """Return the binary representation of `text`. If `text` is already a bytes object, return it as-is.""" if isinstance(text, bytes): return text if not isinstance(text, six.string_types): raise TypeError('to_bytes must receive a unicode, str or bytes ' 'object, got %s' % type(text).__name__) if encoding is None: encoding = 'utf-8' return text.encode(encoding, errors) def to_native_str(text, encoding=None, errors='strict'): """ Return str representation of `text` (bytes in Python 2.x and unicode in Python 3.x). """ if six.PY2: return to_bytes(text, encoding, errors) else: return to_unicode(text, encoding, errors) w3lib-1.22.0/w3lib.egg-info/0000775000372000037200000000000013657045265016205 5ustar travistravis00000000000000w3lib-1.22.0/w3lib.egg-info/PKG-INFO0000664000372000037200000000170213657045265017302 0ustar travistravis00000000000000Metadata-Version: 1.1 Name: w3lib Version: 1.22.0 Summary: Library of web-related functions Home-page: https://github.com/scrapy/w3lib Author: Scrapy project Author-email: info@scrapy.org License: BSD Description: UNKNOWN Platform: Any Classifier: Development Status :: 5 - Production/Stable Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Internet :: WWW/HTTP w3lib-1.22.0/w3lib.egg-info/SOURCES.txt0000664000372000037200000000105213657045265020067 0ustar travistravis00000000000000LICENSE MANIFEST.in README.rst pytest.ini setup.cfg setup.py tox.ini docs/Makefile docs/conf.py docs/index.rst docs/make.bat docs/w3lib.rst tests/__init__.py tests/py3-ignores.txt tests/test_encoding.py tests/test_form.py tests/test_html.py tests/test_http.py tests/test_url.py w3lib/__init__.py w3lib/encoding.py w3lib/form.py w3lib/html.py w3lib/http.py w3lib/url.py w3lib/util.py w3lib.egg-info/PKG-INFO w3lib.egg-info/SOURCES.txt w3lib.egg-info/dependency_links.txt w3lib.egg-info/not-zip-safe w3lib.egg-info/requires.txt w3lib.egg-info/top_level.txtw3lib-1.22.0/w3lib.egg-info/dependency_links.txt0000664000372000037200000000000113657045265022253 0ustar travistravis00000000000000 w3lib-1.22.0/w3lib.egg-info/not-zip-safe0000664000372000037200000000000113657045265020433 0ustar travistravis00000000000000 w3lib-1.22.0/w3lib.egg-info/requires.txt0000664000372000037200000000001313657045265020577 0ustar travistravis00000000000000six>=1.4.1 w3lib-1.22.0/w3lib.egg-info/top_level.txt0000664000372000037200000000000613657045265020733 0ustar travistravis00000000000000w3lib