Do html redirect based on header request - javascript

I have near zero html and/or javascript experience and I'm finding so little information on this topic that I suspect I've got a fundamental misunderstanding on what I'm trying to do. But hey, software developers are somehow fungible so this ended up on my plate.
I've got a git project that includes several files. It looks like this:
git repo:
index.html
my_data.json
my_data.html
.github/workflows
some_workflow.yaml
some_folder
some_other_file.json
I want to publish this on github-pages, which expects the page to be at index.html. When a user does a request to the github-pages url the files that get returned should be based off the request header that the user uses.
For example:
Navigating to the webpage http://my-org.github.io/my-project in a
web browser would end up at the index.html file and should be
redirected to my_data.html.
curl -H "Accept: application/json" "http://my-org.github.io/my-project" should return the my_data.json
file.
A real nice bonus would be if I could navigate to http://my-org.github.io/my-project.json and get the json file
Redirecting to the my_data.html file by setting up a meta <http-equiv="refresh" content="0; url='./my_data.html'"> seems pretty standard but I can't figure out how to do the json part. Most tutorials specify what content negototiation is but then get all hand-wavey at the implementation or refer to API frameworks that have it all baked in.
Is there some kind of javascript if statement I could use to handle the TWO mime-types I expect to handle?

Related

JavaScript in requests package python

I want to get text from a site using Python.
But the site uses JavaScript and the requests package to receive only JavaScript code.
Is there a way to get text without using Selenium?
import requests as r
a=r.get('https://aparat.com/').text
If the site loads content using javascript then the javascript has to be run in order to get the content. I ran into this issue a while back when I did some web scraping, and ended up using Selenium. Yes its slower than BeautifulSoup but it's the easiest solution.
If you know how the server works you could send a request and it should return with content of some kind (whether that be html, json, etc)
Edit: Load the developer tools, go to network tab and refresh the page. Look for an XHR request and the URL it uses. You may be able to use this data for your needs.
For example I found these URLs:
https://www.aparat.com/api/fa/v1/etc/page/config/mode/full
https://www.aparat.com/api/fa/v1/video/video/list/tagid/1?next=1
If you navigate to these in your browser you will notice JSON content, you might be able to use this. I think some of the text is encoded in Unicode e.g \u062e\u0644\u0627\u0635\u0647 \u0628\u0627\u0632\u06cc -> خلاصه بازی
I don't know the specific python implementation you might use. Look for libs that support making http requests and recieving data. That way you can avoid selenium. But you must know the URL's beforehand. Like shown above.
For example this is what I would do:
Make a http request to the URL you find in developer tools
With JSON content, use a JSON parser to get a table/array/dictionary natively. You can then traverse this in the native programming language.
Use a unicode decoder to get the text in normal text format, there might be a lib to do this, but for example on this website using the "Decode/Unescape Unicode Entities" I was able to get the text.
I hope this helps.
Sample code:
import requests;
req = requests.get('https://www.aparat.com/api/fa/v1/video/video/show/videohash/IueKs?pr=1&mf=1&referer=direct')
res = req.json()
#do stuff with res
print(res)

how to parse these types of dynamic links

i have some strange question. i know when i visit any folder of site and in that folder if we have index.php then the xyz.com/folder_name will display contents of that folder if index.php file is there.
i just came across one site here
https://santabanta.pushpaddy.com/
when u will load u can see in network tab that they are loading several links as usual but i did not understand this particular link. how this link is being processed.
https://santabanta.pushpaddy.com/check/emj_F9FghmI:APA91bHsomenoandtextHn2qOPwvs-Jti-pAR1vYDsomenoandtextx2lWwyyYP-Ez1kQsomenoandtextl94nQdmOZEOGYVnA-cVhum6YrN0ZFLUIqu-PmGXMecysomenoandtextc9vvmuyxQ
after check you can see that its generated dynamically and how the server will process these data??
defenitely nothing is there in index.php under check folder. so how it is working. if you will open that link directly it will give that 404 error but when i reload that page
https://santabanta.pushpaddy.com/
it fetches content from check link with 200 code means all are fine.
i know it looks like silly question but i really want to learn this part
As per my understanding this is a token(Like JWT) directly attache to url so that if you open url the server side code will read token and convert the token from encrypted to decrypted and use this information to perform further logic.

Redirect from file based on referrer using JavaScript

Versions of this question have been posted numerous times, but none of the solutions I've found on this site have worked so far. I'm trying to redirect away from files, not web pages. I actually need to know if this is even possible, since I learned that PHP is incapable of doing this. Here's an answer from a previous question I asked:
The web server will first check if it exists and if it does, it will serve the file immediately. It does not go through any PHP code. Therefore you cannot write any PHP code that will intercept this request and block it.
We have a folder on our site with a path of /downloads/, containing files we don't want just anyone to download.
I want to put a script in our main JavaScript file that says:
If file is is /downloads/
If user comes from referrer allowed_domain.com, allow access to files in /downloads/
Else redirect to homepage or 404
My attempt (didn't work)
if (top.location.pathname === '/downloads/private.zip') {
if (document.referrer !== "http://www.allowed_domain.com") {
document.location.path = "/downloads/private.zip";
}
else {
document.location.path = "/404";
}
}
Constraints
I cannot use .htaccess. Our hosting provider is running Nginx, not Apache. I've tried using Nginx config code, but I have to send it to them to implement, and it didn't work and they won't help me.
And yes, I know that this is a super, super insecure solution for restricting access. My company is working on a more formal solution, but until then, I need to implement something temporary to deter users who lack the computer knowledge or motivation to get around the redirect, and this is pretty much my last option.
This problem is not solvable in JavaScript, even in the very limited and insecure way that you are proposing. The problem is that a request to /downloads/private.zip directly returns the contents of that file - it doesn't load any HTML page, so the browser will never see or execute that JavaScript code.
A way to solve this would be to have a PHP file that handles any request to that directory, checks whether the user has permission to see those files, and then returns the requested file or a 404. But for that you need some form of configuration, and you've already told us you can't do that either.
A third solution, one that is very silly but would work (for unsavvy users) in this very constrained situation would be to replace all links to the forbidden resources with a snippet of JavaScript that directs the user either to the file or a 404 page. However, from your question it seems very likely that you're trying to prevent access from users coming from sites outside of your control, in which case this won't work either.
Bottom line: This is not a solvable problem if you don't have the ability to configure your web server.

How to replace post ID with url slug

I have a DB table that saves my title as a slug (ie: this-is-my-title-slug) but I have no clue how to use it in the url.
Example ofor current url http://www.example.com/post.php?id=102
What I want is
http://www.example.com/this-is-a-slug.
You're looking at SEO urls. And a slug is something you would use as a name of an article on www.example.com/articles/article-title-slug for instance, what you probably mean is a permalinks which basically are SEO urls.
If you're running an apache server with PHP and you have a vhost setup then you can use that vhost to allow url rewriting (Apache mod_rewrite).
This allows you to place a file called .htaccess in the webroot of your project (e.g. same directory as root index.php file), within this file you can set up rules that allow such rewriting. What often happens is that you rewrite everything to the root index.php file and you capture the URI and parse the parameters by splitting it on the forward slash (/) character. This is basically a router idea.
The advantage of parsing the URI yourself is that you don't really have to do much in the .htaccess file which is much more complex to handle than building a router in PHP itself as well.
A good example of a .htaccess file can be found on this SO answer
about how to get going with that. There are also plenty of other tutorials to get started with this other than that answer.
DigitalOcean - How to use the .htaccess file
TutsPlus - The ultimate guide to .htaccess
It's been a while since I've done it myself but if you have questions, just ask them and I'll see if I can help but anyways, this should point you in the right direction.

Javascript caching and load if modified

I'm developing widget based script based on javascript, where the end user will come to my site copy the script from my widget site something as follows and paste into their site.
<script src="text.js"></script>
The problem im facing is if i modify anything in test.js file from my side, the changes is not reflecting in users site because of js file is being cached users browsers.This test.js is hosted in apache server.
Im have tried many solutions using .htaccess to set Etags,Cache-control but none of them work. Is there any headers that i need to add/modify in .htaccess. The solutions im looking for is request should check for last modified and load the new js if modified, else load from browser cache(Preferably handle with .htaccess).
This is one of the sample .htaccess i tried .
Header set Cache-Control "max-age=864000, public, must-revalidate"
FileETag MTime Size
Regards,
Karthi Kumar

Categories