Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active December 22, 2015 17:39
Show Gist options
  • Save kaecy/6507909 to your computer and use it in GitHub Desktop.
Save kaecy/6507909 to your computer and use it in GitHub Desktop.
require_relative 'webc';
require 'uri';
ws = Webclient.new 'codepad.org';
begin
ws.start "/",
URI.encode_www_form(
'code' => IO.read(ARGV[0]),
'lang' => ARGV[1] == "/p" ? "Plain Text" : 'Ruby',
'submit' => 'Submit'
),
"POST";
puts ws.headers["location"];
rescue Errno::ENOENT
warn "The file you are trying to upload doesn't exist.";
end
require "socket";
class Webclient
attr_reader :initial_line, :headers, :content;
def initialize server, port=80
@socket = TCPSocket.new server, port;
@server = server;
end
def start location, data, method
@socket.write "#{method} #{location} HTTP/1.0\r\n";
@socket.write "Host: #{@server}\r\n";
if method == "POST"
@socket.write "Content-Type: application/x-www-form-urlencoded\r\n";
end
@socket.write "Content-Length: #{data.length}\r\n";
@socket.write "\r\n";
@socket.write data;
@initial_line = @socket.readline;
@headers = {};
while (line = @socket.readline) != "\r\n"
@headers.store *line.downcase.strip.split(/:\s+/);
end
@content = @socket.read;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment