0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redirect URL ... Ruby / Python / golang / Nim

Last updated at Posted at 2021-08-23

#cURL

>curl -I -s 'http://idontknow.where.to.go/?redirect="' | grep 'location'

##Ruby

require 'net/http'
require 'uri'

target_url ="http://idontknow.where.to.go/?redirect="

uri = URI.parse(target_url)

def get_url(uri)
    redirect = Net::HTTP.get_response(uri)['location']
    return redirect
end

redirect_url = get_url(uri)

puts redirect_url

##Python

import requests

target_url = "http://idontknow.where.to.go/?redirect="

res = requests.get(target_url)
redirect_url = res.url

Print(redirect_url)
;;hy 1.0a3 using CPython(default)

=> (import re)
=> (import requests)
=> (defn redirect [url]
... (setv res (requests.head url :allow_redirects False))
... (setv redirect_url (get res.headers "Location"))
... (print redirect_url)))
=> (redirect "http://idontknow.where.to.go/?redirect")

Or use http://pycurl.io/

import pycurl

c = pycurl.Curl()

c.setopt(c.URL, 'http://idontknow.where.to.go/?redirect')
# Follow redirect.
c.setopt(c.FOLLOWLOCATION, 0) # True: 1 / False : 0 
c.perform()

print(c.getinfo(c.REDIRECT_URL))
print(c.getinfo(c.EFFECTIVE_URL))
c.close()

##Golang

package main

import (
    "fmt"
    "net/http"
    )

func main(){
    const target_URL = "https://idontknow.where.to.go/?redirect="

    resp, _ := http.Get(target_URL)
    defer func() {
        resp.Body.Close()
    }()

    redirect_URL := resp.Request.URL.String()                                        
    fmt.Println(redirect_URL)
}


##Nim-lang

import httpclient

proc getRedirectTarget(domain: string): string =
  let client = newHttpClient(maxRedirects = 0)
  var redirected_url = domain
  var resp = client.head(redirected_url)
  while resp.headers.hasKey("location"):
    redirected_url = resp.headers["location"]
    #echo "redirected: ", redirected_url
    resp = client.head(redirected_url)
  return redirected_url

let targetUrl = "http://idontknow.where.to.go/?redirect="

echo "redirect: ", getRedirectTarget(targetUrl)


##Julia-lang

暫定的解決

using Pkg
Pkg.add("HTTP")
using HTTP

resHeader = HTTP.head("http://idontknow.where.to.go/?redirect=",redirect=false)
m = match(r"(?<=Location:\s).+(?=\r)",String(resHeader))
url_text = m.match

たぶん Location 取り出す方法は別にあるはず。


Lua

http client はいくつかあるが、いちばん依存関係に問題が起こらなかったのは、Lua-cURLv3
Lua-cURLv3 は CURL なので、たぶん Lua が使える環境であればインストールで問題は起きない。他は環境(OS / CPU )によってはコンパイルで依存するパッケージが非対応であったりすることが頻発するので、必ずしも便利なパッケージが使えるとは限らない。Lua-cURLv3 は、ドキュメントみても大事なことは書かれていないために、わかっている人が使うという感じだが、ドキュメントが親切な Python の pycurl を参照して概要をつかんでから調べると、ほぼ同じような使用法。だが、命名規則が CURL と違うので、メンテナンスしている人に質問するしかない。調べ方が悪くてわからない...ということではなく、知らないとわからないということでも、書いてない。

local curl = require('cURL')

local easy = curl.easy{
  url            = "http://idontknow.where.to.go/?redirect=",
  --followlocation = true,  -- true: 0
  followlocation = 0, -- false: 0
  maxredirs      = 3,
}

local buffer = {}
easy:setopt_writefunction(table.insert, buffer)
easy:perform()

local res_code = easy:getinfo_response_code()
local res_location = easy:getinfo_redirect_url()

----print(table.concat(buffer))

print('code: ',    res_code)
print('redirect:', res_location)

print('url: ', easy:getinfo_effective_url())
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?