Skip to content

Instantly share code, notes, and snippets.

@jasonmoo
Last active January 3, 2016 15:19
Show Gist options
  • Save jasonmoo/8481743 to your computer and use it in GitHub Desktop.
Save jasonmoo/8481743 to your computer and use it in GitHub Desktop.
convert integer http status codes in WriteHeader to http.Status* constants
<?php
// w.WriteHeader(200) -> w.WriteHeader(http.StatusOK) // 200
array_map(function($a) {
$data = file_get_contents($a);
$data = preg_replace_callback("/\.WriteHeader\((\d+)\)/", function($d) {
static $constants = array(
100 => 'http.StatusContinue',
101 => 'http.StatusSwitchingProtocols',
200 => 'http.StatusOK',
201 => 'http.StatusCreated',
202 => 'http.StatusAccepted',
203 => 'http.StatusNonAuthoritativeInfo',
204 => 'http.StatusNoContent',
205 => 'http.StatusResetContent',
206 => 'http.StatusPartialContent',
300 => 'http.StatusMultipleChoices',
301 => 'http.StatusMovedPermanently',
302 => 'http.StatusFound',
303 => 'http.StatusSeeOther',
304 => 'http.StatusNotModified',
305 => 'http.StatusUseProxy',
307 => 'http.StatusTemporaryRedirect',
400 => 'http.StatusBadRequest',
401 => 'http.StatusUnauthorized',
402 => 'http.StatusPaymentRequired',
403 => 'http.StatusForbidden',
404 => 'http.StatusNotFound',
405 => 'http.StatusMethodNotAllowed',
406 => 'http.StatusNotAcceptable',
407 => 'http.StatusProxyAuthRequired',
408 => 'http.StatusRequestTimeout',
409 => 'http.StatusConflict',
410 => 'http.StatusGone',
411 => 'http.StatusLengthRequired',
412 => 'http.StatusPreconditionFailed',
413 => 'http.StatusRequestEntityTooLarge',
414 => 'http.StatusRequestURITooLong',
415 => 'http.StatusUnsupportedMediaType',
416 => 'http.StatusRequestedRangeNotSatisfiable',
417 => 'http.StatusExpectationFailed',
418 => 'http.StatusTeapot',
500 => 'http.StatusInternalServerError',
501 => 'http.StatusNotImplemented',
502 => 'http.StatusBadGateway',
503 => 'http.StatusServiceUnavailable',
504 => 'http.StatusGatewayTimeout',
505 => 'http.StatusHTTPVersionNotSupported',
);
return ".WriteHeader(".$constants[$d[1]].") // ".$d[1];
}, $data);
file_put_contents($a, $data);
}, glob("*.go"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment