Ed de Almeida Ed de Almeida - 9 days ago 5
HTTP Question

How may I detect requests generated by other requests using Ruby and Puma?

Let me try to explain what I need.

When I include a library like Bootstrap in my HTML page and this page is loaded, the library main files are also loaded. Most of the times the library files require other files. Since I used Bootstrap as an example, let's consider

glyphicons-halflings-regular.svg
,
glyphicons-halflings-regular.ttf
,
glyphicons-halflings-regular.woff
and
glyphicons-halflings-regular.woff2
, all of them part of Bootstrap.

I am writing a Rack middleware to deal with the requests from my application. One of the tasks of this middleware is to modify the requests according to certain conventions. For instance, in my application Bootstrap would be located at this path

ENV['HOME']/my_app/web/libraries/bootstrap/


and its main CSS file would then be

ENV['HOME']/my_app/web/libraries/bootstrap/css/bootstrap.css


And, when loaded, it would generate new requests for all these files I mentioned above.

What I need is identify that a certain file, say
glyphicons-halflings-regular.svg
, was required by another one, so I may use my Rack middleware to change its location accordingly. Like:

Requested file:
`glyphicons-halflings-regular.svg`
Requested-by:
http://my_app_domain.com/web/libraries/bootstrap/css/bootstrap.css
Request address:
http://my_domain_app.com/web/libraries/bootstrap/css/../fonts/glyphicons-halflings-regular.svg


I though about starting a thread to each new request. Then I would have only to consider the address of the main file as the base-address and prepend it to every sub-request generated by that file.

I would love to hear your ideas about this. Any suggestion of a better way to do the same?

EDIT: (spickermann's question)

No, this is not the point. Maybe I wasn't clear, then I'll try to put it in other words.

Consider the following structure:

ENV['HOME']/my_app/web/libraries/library1/
| |
| |_ a_css_file.css
| |_ a_jpeg_file.jpg
|
/library2/
|
|_ another_css_file.css
|_ a_jpeg_file.jpg


Let's assume my middleware receives a request for a file named
a_jpeg_file.jpg
. We have two files with the same name, one in
library1
and other in
library2
.

The only way for me to decide which file to serve is if I may know if
a_jpeg_file.jpg
was required by
a_css_file.css
or by
another_css_file.css
.

In the first case I'd prepend the path to
library1
to the file and serve
ENV['HOME']/my_app/web/libraries/library1/
. In the other case I prepend the path to
library2
and serve
ENV['HOME']/my_app/web/libraries/library2/
.

In both cases I need to know which file generated the request for
a_jpeg_file.jpg
.

In other words, I need to know which library-context required a certain file.

Answer

Maybe you can parametrize your resource, like my-image.jpg?dir='dir1' and parse the params on your middleware. Similar approach is done by Rails to cache the assets. Also you can use some crypt function that encrypt and decrypt the info in the params, like image.jpt?info=HughYF65fFj7t... and then you decrypt the info in your midleware and use the info you sent.