• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

http functions in Essentials

Status
Not open for further replies.

Ho-oh 112

Advance Scripter
311
Posts
13
Years
    • Seen Mar 8, 2014
    Right so I was going through the Sockets scripts and I FOUND AN ARRAY OF "http functions" allowing you to download/post data (I knew about the download data but the posting?)

    I bet people could use this for trades as an alternative from mySQL....
    I'm pretty sure Poccil made this....
    Code:
    #############################
    #
    # HTTP utility functions
    #
    #############################
    def pbPostData(url, postdata, filename=nil, depth=0)
      userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
      if url[/^http:\/\/([^\/]+)(.*)$/]
        host=$1
        path=$2
        path="/" if path.length==0
        body = postdata.map {|key, value|
           keyString=key.to_s
           valueString=value.to_s
           keyString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
           valueString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
           next "#{keyString}=#{valueString}"
        }.join('&')
        request="POST #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma: no-cache\nHost: #{host}\nProxy-Connection: Close\n"
        request+="Content-Type: application/x-www-form-urlencoded\n"
        request+="Content-Length: #{body.length}\n"
        request+="\n"
        request+=body
        return pbHttpRequest(host, request, filename, depth)
      end
      return ""
    end
    def pbDownloadData(url, filename=nil, depth=0)
      userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
      if depth>10
        raise "Redirection level too deep"
      end
      if url[/^http:\/\/([^\/]+)(.*)$/]
        host=$1
        path=$2
        path="/" if path.length==0
        request="GET #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma: no-cache\nHost: #{host}\nProxy-Connection: Close\n\n"
        return pbHttpRequest(host, request, filename, depth)
      end
      return ""
    end
    def pbHttpRequest(host, request, filename=nil, depth=0)
      if depth>10
        raise "Redirection level too deep"
      end
      socket=::TCPSocket.new(host, 80)
      time=Time.now.to_i
      begin
        socket.send(request)
        result=socket.gets
        data=""
        # Get the HTTP result
        if result[/^HTTP\/1\.[01] (\d+).*/]
          errorcode=$1.to_i
          if errorcode>=400 && errorcode<500
            raise "HTTP Error #{errorcode}"
          end
          headers={}
          # Get the response headers
          while true
            result=socket.gets.sub(/\r$/,"")
            break if result==""
            if result[/^([^:]+):\s*(.*)/]
              headers[$1]=$2
            end
          end
          length=-1
          chunked=false
          if headers["Content-Length"]
            length=headers["Content-Length"].to_i
          end
          if headers["Transfer-Encoding"]=="chunked"
            chunked=true
          end
          if headers["Location"] && errorcode >= 300 && errorcode < 400
            socket.close rescue socket=nil
            return pbDownloadData(headers["Location"],filename,depth+1)
          end
          if chunked==true
            # Chunked content
            while true
              lengthline=socket.gets.sub(/\r$/,"")
              length=lengthline.to_i(16)
              break if length==0
              while Time.now.to_i-time>=5 || socket.select(10)==0
                time=Time.now.to_i
                Graphics.update
              end
              data+=socket.recv(length)
              socket.gets
            end
          elsif length==-1
            # No content length specified
            while true
              if socket.select(500)==0
                break
              else
                while Time.now.to_i-time>=5 || socket.select(10)==0
                  time=Time.now.to_i
                  Graphics.update
                end
                data+=socket.recv(1)
              end
            end
          else
            # Content length specified
            while length>0
              chunk=[length,4096].min
              while Time.now.to_i-time>=5 || socket.select(10)==0
                time=Time.now.to_i
                Graphics.update
              end
              data+=socket.recv(chunk)
              length-=chunk
            end
          end
        end
        if filename
          File.open(filename,"wb"){|f|
             f.write(data)
          }
        else
          return data
        end
        ensure
        socket.close rescue socket=nil
      end
      return ""
    end
    def pbDownloadToString(url)
      begin
        data=pbDownloadData(url)
        return data
        rescue
        return ""
      end 
    end
    def pbDownloadToFile(url, file)
      begin
        pbDownloadData(url,file)
        rescue
      end
    end
    def pbPostToString(url, postdata)
      begin
        data=pbPostData(url, postdata)
        return data
        rescue
        return ""
      end 
    end
    def pbPostToFile(url, postdata, file)
      begin
        pbPostData(url, postdata,file)
        rescue
      end
    end

    HOW COULD I MISS THIS???
     
    10,673
    Posts
    15
    Years
    • Seen Dec 30, 2023
    I'd like you to clarify the topic of this discussion and post it again. All you've done is post a script available in essentials and say what it might be useful for, but that's not enough to go on. I don't know why you've entitled this with the "Misc" prefix. What is this thread for? I have no idea. You need to actually have a discussion topic, you shouldn't just post scripts and throw out an extremely vague topic around it.

    Please make a new thread with a topic and a direction to go in. If you're hoping to convert this into a trade script, then state it in the discussion and ask how people could approach this. Then use the prefix [Discussion].

    Locked.
     
    Status
    Not open for further replies.
    Back
    Top