# Path: restkit/util.py
# def to_bytestring(s):
#     if not isinstance(s, basestring):
#         raise TypeError("value should be a str or unicode")
# 
#     if isinstance(s, unicode):
#         return s.encode('utf-8')
#     return s


import base64
import cgi
import os
import socket
import tempfile
import threading
import unittest
import urlparse
import Cookie
import urllib
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    from urlparse import parse_qsl, parse_qs
    from cgi import parse_qsl, parse_qs
from restkit.util import to_bytestring
    def do_HEAD(self):
        if self.path == "/ok":
            extra_headers = [('Content-type', 'text/plain')]
            self._respond(200, extra_headers, '')
        else:
            self.error_Response()

    def error_Response(self, message=None):
        req = [
            ('HTTP method', self.command),
            ('path', self.path),
            ]
        if message:
            req.append(('message', message))

        body_parts = ['Bad request:\r\n']
        for k, v in req:
            body_parts.append(' %s: %s\r\n' % (k, v))
        body = ''.join(body_parts)
        self._respond(400, [('Content-type', 'text/plain'),
        ('Content-Length', str(len(body)))], body)


    def _respond(self, http_code, extra_headers, body):
        self.send_response(http_code)
        keys = []
        for k, v in extra_headers:
            self.send_header(k, v)
            keys.append(k)
        if body:

# Path: restkit/util.py
# def to_bytestring(s):
#     if not isinstance(s, basestring):
#         raise TypeError("value should be a str or unicode")
# 
#     if isinstance(s, unicode):
#         return s.encode('utf-8')
#     return s


# Path: tests/_server_test.py
import base64
import cgi
import os
import socket
import tempfile
import threading
import unittest
import urlparse
import Cookie
import urllib
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    from urlparse import parse_qsl, parse_qs
    from cgi import parse_qsl, parse_qs
from restkit.util import to_bytestring

    def do_HEAD(self):
        if self.path == "/ok":
            extra_headers = [('Content-type', 'text/plain')]
            self._respond(200, extra_headers, '')
        else:
            self.error_Response()

    def error_Response(self, message=None):
        req = [
            ('HTTP method', self.command),
            ('path', self.path),
            ]
        if message:
            req.append(('message', message))

        body_parts = ['Bad request:\r\n']
        for k, v in req:
            body_parts.append(' %s: %s\r\n' % (k, v))
        body = ''.join(body_parts)
        self._respond(400, [('Content-type', 'text/plain'),
        ('Content-Length', str(len(body)))], body)


    def _respond(self, http_code, extra_headers, body):
        self.send_response(http_code)
        keys = []
        for k, v in extra_headers:
            self.send_header(k, v)
            keys.append(k)
        if body:

            body = to_bytestring(body)