Skip to content

pulpcore.plugin.content

The Content app provides built-in functionality to handle user requests for content, but in some cases the default behavior may not work for some content types. For example, Container content requires specific response headers to be present. In these cases the plugin write should provide a custom Handler to the Content App by subclassing pulpcore.plugin.content.Handler.

Making a custom Handler is a two-step process:

  1. subclass pulpcore.plugin.content.Handler to define your Handler's behavior
  2. Add the Handler to a route using aiohttp.server's add_route() interface.

If content needs to be served from within the Distribution's base_path, overriding the pulpcore.plugin.models.Distribution.content_handler and pulpcore.plugin.models.Distribution.content_handler_directory_listing methods in your Distribution is an easier way to serve this content. The pulpcore.plugin.models.Distribution.content_handler method should return an instance of aiohttp.web_response.Response or a pulpcore.plugin.models.ContentArtifact.

Creating your Handler

Import the Handler object through the plugin API and then subclass it. Custom functionality can be provided by overriding the various methods of Handler, but here is the simplest version:

from pulpcore.plugin.content import Handler

class MyHandler(Handler):

    pass

Here is an example of the Container custom Handler.

Registering your Handler

We register the Handler with Pulp's Content App by importing the aiohttp.server 'app' and then adding a custom route to it. Here's an example:

from pulpcore.content import app

app.add_routes([web.get(r'/my/custom/{somevar:.+}', MyHandler().stream_content)])

Here is an example of Container registering some custom routes.

Restricting which detail Distributions Match

To restrict which Distribution model types your Handler will serve, set the distribution_model field to your Model type. This causes the Handler to only search/serve your Distribution types.

from pulpcore.plugin.content import Handler

from models import MyDistribution


class MyHandler(Handler):

    distribution_model = MyDistribution

pulpcore.plugin.content.Handler

pulpcore.plugin.content.Handler

A default Handler for the Content App that also can be subclassed to create custom handlers.

This Handler will perform the following:

  1. Match the request against a Distribution

  2. Call the certguard check if a certguard exists for the matched Distribution.

  3. If the Distribution has a publication serve that Publication's PublishedArtifacts, PublishedMetadata by the remaining relative path. If still unserved and if pass_through is set, the associated repository_version will have its ContentArtifacts served by relative_path also. This will serve the associated Artifact.

  4. If still unmatched, and the Distribution has a repository attribute set, find it's latest repository_version. If the Distribution has a repository_version attribute set, use that. For this repository_version, find matching ContentArtifact objects by relative_path and serve them. If there is an associated Artifact return it.

  5. If the Distribution has a remote, find an associated RemoteArtifact that matches by relative_path. Fetch and stream the corresponding RemoteArtifact to the client, optionally saving the Artifact depending on the policy attribute.

list_distributions(request) async

The handler for an HTML listing all distributions

Raises:

  • [aiohttp.web.HTTPOk][]

    The response back to the client.

  • [PathNotResolved][]

    404 error response when path doesn't exist.

find_base_path_cached(request, cached) async classmethod

Finds the base-path to use for the base-key in the cache

Returns:

  • str

    The base-path associated with this request

auth_cached(request, cached, base_key) async classmethod

Authentication check for the cached stream_content handler

Parameters:

  • base_key (str) –

    The base_key associated with this response

stream_content(request) async

The request handler for the Content app.

Returns:

  • [aiohttp.web.StreamResponse][] or [aiohttp.web.FileResponse][]: The response back to the client.

response_headers(path, distribution=None) staticmethod

Get the Content-Type and Encoding-Type headers for the requested path.

Parameters:

  • path (str) –

    The relative path that was requested.

  • distribution(Distribution)

    Distribution detail that might want to add headers for path

Returns: headers (dict): A dictionary of response headers.

render_html(directory_list, path='', dates=None, sizes=None) staticmethod

Render a list of strings as an HTML list of links.

Parameters:

  • directory_list (iterable) –

    an iterable of strings representing file and directory names

Returns:

  • String representing HTML of the directory listing.

list_directory(repo_version, publication, path) async

Generate a set with directory listing of the path.

This method expects either a repo_version or a publication in addition to a path. This method generates a set of strings representing the list of a path inside the repository version or publication.

Parameters:

  • path (str) –

    relative path inside the repo version of publication.

Returns:

  • Set of strings representing the files and directories in the directory listing.