Skip to content

Add support for the protocol relative URLs for resources. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions grails-app/taglib/org/grails/plugin/resource/ResourceTagLib.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ResourceTagLib {

}
]

static writeAttrs( attrs, output) {
// Output any remaining user-specified attributes
attrs.each { k, v ->
Expand Down Expand Up @@ -445,7 +445,7 @@ class ResourceTagLib {
def stash = { attrs, body ->
stashPageFragment(attrs.type, attrs.disposition, body())
}

protected getModuleByName(name) {
def module = grailsResourceProcessor.getModule(name)
if (!module) {
Expand Down Expand Up @@ -485,7 +485,7 @@ class ResourceTagLib {
def debugMode = grailsResourceProcessor.isDebugMode(request)

for (r in module.resources) {
if (!r.exists() && !r.actualUrl?.contains('://')) {
if (!r.exists() && !URLUtils.isGlobalAbsolute(r.actualUrl)) {
throw new IllegalArgumentException("Module [$name] depends on resource [${r.sourceUrl}] but the file cannot be found")
}
if (log.debugEnabled) {
Expand Down Expand Up @@ -525,7 +525,7 @@ class ResourceTagLib {
}
def ctxPath = request.contextPath
def uri = attrs.remove('uri')
def abs = uri?.indexOf('://') >= 0
def abs = URLUtils.isGlobalAbsolute(uri)

if (!uri || !abs) {
if (uri) {
Expand All @@ -535,7 +535,7 @@ class ResourceTagLib {
// via g.resource
attrs.contextPath = ctxPath
uri = grailsLinkGenerator.resource(attrs)
abs = uri.contains('://')
abs = URLUtils.isGlobalAbsolute(uri)
}
}

Expand Down Expand Up @@ -587,12 +587,13 @@ class ResourceTagLib {
}

// If the link has to support linkUrl for override, or fall back to the full requested url
// we resolve without query params, but must keep them for linking
def linkUrl = res ? res.linkUrl : contextRelUri
// we resolve without query params, but must keep them for linking
def linkUrl = res ? res.linkUrl : reluri

if (linkUrl.contains('://')) {
def baseUrl = '' // @todo get from config
if (URLUtils.isGlobalAbsolute(linkUrl) || baseUrl) {
// @todo do we need to toggle http/https here based on current request protocol?
return [uri:linkUrl, resource:res]
return [uri:baseUrl ? baseUrl+linkUrl : linkUrl, resource:res]
} else {
// Only apply static prefix if the resource actually has ResourceMeta created for it
uri = res ? ctxPath+grailsResourceProcessor.staticUrlPrefix+linkUrl : ctxPath+linkUrl
Expand Down Expand Up @@ -635,7 +636,7 @@ class ResourceTagLib {
def o = new StringBuilder()
o << "<img src=\"${info.uri.encodeAsHTML()}\" "
def attribs = res?.tagAttributes ? res.tagAttributes.clone() : [:]
def excludes = ['dir', 'uri', 'file', 'plugin']
def excludes = ['dir', 'uri', 'file', 'plugin']
attribs += attrs.findAll { !(it.key in excludes) }
attrs = attribs

Expand Down
6 changes: 3 additions & 3 deletions src/groovy/org/grails/plugin/resource/ResourceMeta.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class ResourceMeta {
boolean isActualAbsolute() {
actualUrl.indexOf(':/') > 0
}

boolean isDirty() {
!originalResource ||
(originalResource.lastModified() != originalLastMod)
Expand Down Expand Up @@ -212,7 +212,7 @@ class ResourceMeta {
// Hook for when preparation is starting
void beginPrepare(grailsResourceProcessor) {
def uri = this.sourceUrl
if (!uri.contains('://')) {
if (!URLUtils.isGlobalAbsolute(uri)) {

// Delete whatever file may already be there
processedFile?.delete()
Expand Down Expand Up @@ -347,7 +347,7 @@ class ResourceMeta {
return null
}
}

String getActualUrlParent() {
def lastSlash = actualUrl.lastIndexOf('/')
if (lastSlash >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ResourceModule {
ResourceMeta newResourceFromArgs(Map args, svc, boolean singleResourceModule) {
def url = args.remove('url')
if (url) {
if (!url.contains('://') && !url.startsWith('/')) {
if (!URLUtils.isGlobalAbsolute(url) && !url.startsWith('/')) {
url = '/'+url
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/groovy/org/grails/plugin/resource/URLUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@ class URLUtils {
!url.startsWith('#') &&
!(url.indexOf('://') >= 0)
}

/**
* Works out if url is globally absolute, as in it begins with a protocol
* like http:// or just //
*/
static Boolean isGlobalAbsolute(url) {
url?.indexOf('://') >= 0 ||
url?.startsWith('//')
}
}