Skip to content

Commit

Permalink
change encoding on mch.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuktommy committed Jan 12, 2015
1 parent 643200a commit 207bf06
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
38 changes: 32 additions & 6 deletions shingetsu/mch/datd.py
@@ -1,4 +1,30 @@
'2ch like dat interface'
"""2ch like dat interface
"""
#
# Copyright (c) 2014,2015 shinGETsu Project.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

from wsgiref import simple_server
import threading
Expand Down Expand Up @@ -111,7 +137,7 @@ def board_app(env, resp):
'<h1>%s - %s</h1>' % (message['logo'], message['description']),
'</body></html>',
]
return ((c + '\n').encode('sjis', 'ignore') for c in html)
return ((c + '\n').encode('cp932', 'replace') for c in html)


def thread_app(env, resp):
Expand Down Expand Up @@ -139,7 +165,7 @@ def thread_app(env, resp):
headers['Last-Modified'] = last_m
resp("200 OK", headers.items())

return (c.encode('sjis', 'ignore') for c in thread)
return (c.encode('cp932', 'replace') for c in thread)



Expand Down Expand Up @@ -167,16 +193,16 @@ def subject_app(env, resp):

resp('200 OK', [('Content-Type', 'text/plain; charset=Shift_JIS'),
('Last-Modified', eutils.formatdate(last_stamp))])
return (s.encode('sjis', 'ignore') for s in subjects)
return (s.encode('cp932', 'replace') for s in subjects)


def head_app(env, resp):
resp('200 OK', [('Content-Type', 'text/plain; charset=Shift_JIS')])
body = []
with open(config.motd, encoding='utf-8', errors='ignore') as f:
with open(config.motd, encoding='utf-8', errors='replace') as f:
for line in f:
body.append(line.rstrip('\n') + '<br>\n')
return [''.join(body).encode('sjis')]
return [''.join(body).encode('cp932', 'replace')]


class Datd(threading.Thread):
Expand Down
12 changes: 7 additions & 5 deletions shingetsu/mch/post.py
Expand Up @@ -43,8 +43,10 @@ def post_comment(thread_key, name, mail, body, passwd):

def error_resp(msg, start_response, host, name, mail, body):
info = {'message': msg, 'host': host, 'name': name, 'mail': mail, 'body': body}
msg = template.Template().display('2ch_error', info).encode('sjis')
start_response('200 OK', [('Content-Type', 'text/html; charset=shift_jis')])
msg = (template.Template()
.display('2ch_error', info)
.encode('cp932', 'replace'))
start_response('200 OK', [('Content-Type', 'text/html; charset=Shift_JIS')])
return [msg]

success_msg = '''<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html"><title>書きこみました。</title></head>
Expand All @@ -53,7 +55,7 @@ def error_resp(msg, start_response, host, name, mail, body):

def _get_comment_data(env):
fs = cgi.FieldStorage(environ=env, fp=env['wsgi.input'],
encoding='sjis')
encoding='cp932')
prop = lambda s: fs[s].value if s in fs else ''
return [prop('subject'), prop('FROM'), prop('mail'), prop('MESSAGE'), prop('key')]

Expand Down Expand Up @@ -106,6 +108,6 @@ def replace(match):
return error_resp('自ノード以外で署名機能は使えません', resp, **info)

post_comment(key, name, mail, body, passwd)
resp('200 OK', [('Content-Type', 'text/html; charset=shift_jis')])
return [success_msg.encode('sjis')]
resp('200 OK', [('Content-Type', 'text/html; charset=Shift_JIS')])
return [success_msg.encode('cp932', 'replace')]

0 comments on commit 207bf06

Please sign in to comment.