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:
- subclass
pulpcore.plugin.content.Handler
to define your Handler's behavior - 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:
-
Match the request against a Distribution
-
Call the certguard check if a certguard exists for the matched Distribution.
-
If the Distribution has a
publication
serve that Publication'sPublishedArtifacts
,PublishedMetadata
by the remainingrelative path
. If still unserved and ifpass_through
is set, the associatedrepository_version
will have itsContentArtifacts
served byrelative_path
also. This will serve the associatedArtifact
. -
If still unmatched, and the Distribution has a
repository
attribute set, find it's latestrepository_version
. If the Distribution has arepository_version
attribute set, use that. For thisrepository_version
, find matchingContentArtifact
objects byrelative_path
and serve them. If there is an associatedArtifact
return it. -
If the Distribution has a
remote
, find an associatedRemoteArtifact
that matches byrelative_path
. Fetch and stream the correspondingRemoteArtifact
to the client, optionally saving theArtifact
depending on thepolicy
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.