Add maxmind demo to redbean-demo.com

This commit is contained in:
Justine Tunney 2022-03-18 15:31:54 -07:00
parent c4d8f40422
commit c541225af0
9 changed files with 355 additions and 30 deletions

View file

@ -731,19 +731,19 @@ static bool ChunkEq(struct TtyRgb c[hasatleast 4],
}
static struct TtyRgb *CopyChunk(struct TtyRgb chunk[hasatleast 4],
const struct TtyRgb *c, size_t n) {
chunk[TL] = c[0 + 0];
chunk[TR] = c[0 + 1];
chunk[BL] = c[n + 0];
chunk[BR] = c[n + 1];
const struct TtyRgb *c, size_t xn) {
chunk[TL] = c[00 + 0];
chunk[TR] = c[00 + 1];
chunk[BL] = c[xn + 0];
chunk[BR] = c[xn + 1];
return chunk;
}
static dontinline char *CopyRun(char *v, size_t n,
struct TtyRgb lastchunk[hasatleast 4],
const struct TtyRgb **c, size_t *x,
struct TtyRgb *bg, struct TtyRgb *fg,
struct Glyph *glyph) {
struct TtyRgb lastchunk[hasatleast 4],
const struct TtyRgb **c, size_t *x,
struct TtyRgb *bg, struct TtyRgb *fg,
struct Glyph *glyph) {
struct TtyRgb chunk[4];
if (memcmp(glyph, &kGlyphs[1][0], sizeof(*glyph)) == 0) {
if (!ttyeq(*bg, *fg)) {
@ -766,17 +766,18 @@ static dontinline char *CopyRun(char *v, size_t n,
/**
* Maps 2×2 pixel chunks onto ANSI UNICODE cells.
* @note h/t Nick Black for his quadrant blitting work on notcurses
* @note yn and xn need to be even
*/
char *ttyraster(char *v, const struct TtyRgb *c, size_t yn, size_t n,
char *ttyraster(char *v, const struct TtyRgb *c, size_t yn, size_t xn,
struct TtyRgb bg, struct TtyRgb fg) {
unsigned y, x;
struct Pick p;
struct Glyph glyph;
struct TtyRgb chun[4], lastchunk[4];
for (y = 0; y < yn; y += 2, c += n) {
for (y = 0; y < yn; y += 2, c += xn) {
if (y) *v++ = '\r', *v++ = '\n';
for (x = 0; x < n; x += 2, c += 2) {
CopyChunk(chun, c, n);
for (x = 0; x < xn; x += 2, c += 2) {
CopyChunk(chun, c, xn);
if (ttyquant()->alg == kTtyQuantTrue) {
if (ttyquant()->blocks == kTtyBlocksCp437) {
p = PickBlockCp437True(chun[TL], chun[TR], chun[BL], chun[BR]);

View file

@ -18,6 +18,7 @@
*/
#include "libc/bits/popcnt.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/hyperion.h"
#include "libc/testlib/testlib.h"
TEST(popcnt, test) {
@ -31,4 +32,5 @@ BENCH(popcnt, bench) {
EXPROPRIATE(popcnt(VEIL("r", 0x5555555555555555))));
EZBENCH2("(popcnt)", donothing,
EXPROPRIATE((popcnt)(VEIL("r", 0x5555555555555555))));
EZBENCH_N("_countbits", kHyperionSize, _countbits(kHyperion, kHyperionSize));
}

View file

@ -43,7 +43,6 @@ TEST(strerror, einval) {
TEST(strerror, symbolizingTheseNumbersAsErrorsIsHeresyInUnixStyle) {
EXPECT_STARTSWITH("EUNKNOWN", strerror(0));
EXPECT_STARTSWITH("EUNKNOWN", strerror(-1));
}
TEST(strerror, enotconn_orLinkerIsntUsingLocaleC_orCodeIsOutOfSync) {

View file

@ -228,11 +228,7 @@ TEST(ksnprintf, fuzzTheUnbreakable) {
TEST(kprintf, testFailure_wontClobberErrnoAndBypassesSystemCallSupport) {
int n;
#if 0
ASSERT_EQ(0, errno); /* xxx: todo(jart) */
#else
errno = 0;
#endif
ASSERT_EQ(0, errno);
EXPECT_SYS(0, 3, dup(2));
EXPECT_SYS(0, 0, close(2));
n = g_syscount;

313
tool/net/demo/maxmind.lua Normal file
View file

@ -0,0 +1,313 @@
local maxmind = require "maxmind"
local kMetroCodes = {
[500] = "Portland-Auburn ME",
[501] = "New York NY",
[502] = "Binghamton NY",
[503] = "Macon GA",
[504] = "Philadelphia PA",
[505] = "Detroit MI",
[506] = "Boston MA-Manchester NH",
[507] = "Savannah GA",
[508] = "Pittsburgh PA",
[509] = "Ft. Wayne IN",
[510] = "Cleveland-Akron (Canton) OH",
[511] = "Washington DC (Hagerstown MD)",
[512] = "Baltimore MD",
[513] = "Flint-Saginaw-Bay City MI",
[514] = "Buffalo NY",
[515] = "Cincinnati OH",
[516] = "Erie PA",
[517] = "Charlotte NC",
[518] = "Greensboro-High Point-Winston Salem NC",
[519] = "Charleston SC",
[520] = "Augusta GA",
[521] = "Providence RI-New Bedford MA",
[522] = "Columbus GA",
[523] = "Burlington VT-Plattsburgh NY",
[524] = "Atlanta GA",
[525] = "Albany GA",
[526] = "Utica NY",
[527] = "Indianapolis IN",
[528] = "Miami-Ft. Lauderdale FL",
[529] = "Louisville KY",
[530] = "Tallahassee FL-Thomasville GA",
[531] = "Tri-Cities TN-VA",
[532] = "Albany-Schenectady-Troy NY",
[533] = "Hartford & New Haven CT",
[534] = "Orlando-Daytona Beach-Melbourne FL",
[535] = "Columbus OH",
[536] = "Youngstown OH",
[537] = "Bangor ME",
[538] = "Rochester NY",
[539] = "Tampa-St. Petersburg (Sarasota) FL",
[540] = "Traverse City-Cadillac MI",
[541] = "Lexington KY",
[542] = "Dayton OH",
[543] = "Springfield-Holyoke MA",
[544] = "Norfolk-Portsmouth-Newport News VA",
[545] = "Greenville-New Bern-Washington NC",
[546] = "Columbia SC",
[547] = "Toledo OH",
[548] = "West Palm Beach-Ft. Pierce FL",
[549] = "Watertown NY",
[550] = "Wilmington NC",
[551] = "Lansing MI",
[552] = "Presque Isle ME",
[553] = "Marquette MI",
[554] = "Wheeling WV-Steubenville OH",
[555] = "Syracuse NY",
[556] = "Richmond-Petersburg VA",
[557] = "Knoxville TN",
[558] = "Lima OH",
[559] = "Bluefield-Beckley-Oak Hill WV",
[560] = "Raleigh-Durham (Fayetteville) NC",
[561] = "Jacksonville FL",
[563] = "Grand Rapids-Kalamazoo-Battle Creek MI",
[564] = "Charleston-Huntington WV",
[565] = "Elmira NY",
[566] = "Harrisburg-Lancaster-Lebanon-York PA",
[567] = "Greenville-Spartanburg SC-Asheville NC-Anderson SC",
[569] = "Harrisonburg VA",
[570] = "Florence-Myrtle Beach SC",
[571] = "Ft. Myers-Naples FL",
[573] = "Roanoke-Lynchburg VA",
[574] = "Johnstown-Altoona PA",
[575] = "Chattanooga TN",
[576] = "Salisbury MD",
[577] = "Wilkes Barre-Scranton PA",
[581] = "Terre Haute IN",
[582] = "Lafayette IN",
[583] = "Alpena MI",
[584] = "Charlottesville VA",
[588] = "South Bend-Elkhart IN",
[592] = "Gainesville FL",
[596] = "Zanesville OH",
[597] = "Parkersburg WV",
[598] = "Clarksburg-Weston WV",
[600] = "Corpus Christi TX",
[602] = "Chicago IL",
[603] = "Joplin MO-Pittsburg KS",
[604] = "Columbia-Jefferson City MO",
[605] = "Topeka KS",
[606] = "Dothan AL",
[609] = "St. Louis MO",
[610] = "Rockford IL",
[611] = "Rochester MN-Mason City IA-Austin MN",
[612] = "Shreveport LA",
[613] = "Minneapolis-St. Paul MN",
[616] = "Kansas City MO",
[617] = "Milwaukee WI",
[618] = "Houston TX",
[619] = "Springfield MO",
[622] = "New Orleans LA",
[623] = "Dallas-Ft. Worth TX",
[624] = "Sioux City IA",
[625] = "Waco-Temple-Bryan TX",
[626] = "Victoria TX",
[627] = "Wichita Falls TX & Lawton OK",
[628] = "Monroe LA-El Dorado AR",
[630] = "Birmingham AL",
[631] = "Ottumwa IA-Kirksville MO",
[632] = "Paducah KY-Cape Girardeau MO-Harrisburg-Mount Vernon IL",
[633] = "Odessa-Midland TX",
[634] = "Amarillo TX",
[635] = "Austin TX",
[636] = "Harlingen-Weslaco-Brownsville-McAllen TX",
[637] = "Cedar Rapids-Waterloo-Iowa City & Dubuque IA",
[638] = "St. Joseph MO",
[639] = "Jackson TN",
[640] = "Memphis TN",
[641] = "San Antonio TX",
[642] = "Lafayette LA",
[643] = "Lake Charles LA",
[644] = "Alexandria LA",
[647] = "Greenwood-Greenville MS",
[648] = "Champaign & Springfield-Decatur,IL",
[649] = "Evansville IN",
[650] = "Oklahoma City OK",
[651] = "Lubbock TX",
[652] = "Omaha NE",
[656] = "Panama City FL",
[657] = "Sherman TX-Ada OK",
[658] = "Green Bay-Appleton WI",
[659] = "Nashville TN",
[661] = "San Angelo TX",
[662] = "Abilene-Sweetwater TX",
[669] = "Madison WI",
[670] = "Ft. Smith-Fayetteville-Springdale-Rogers AR",
[671] = "Tulsa OK",
[673] = "Columbus-Tupelo-West Point MS",
[675] = "Peoria-Bloomington IL",
[676] = "Duluth MN-Superior WI",
[678] = "Wichita-Hutchinson KS",
[679] = "Des Moines-Ames IA",
[682] = "Davenport IA-Rock Island-Moline IL",
[686] = "Mobile AL-Pensacola (Ft. Walton Beach) FL",
[687] = "Minot-Bismarck-Dickinson(Williston) ND",
[691] = "Huntsville-Decatur (Florence) AL",
[692] = "Beaumont-Port Arthur TX",
[693] = "Little Rock-Pine Bluff AR",
[698] = "Montgomery (Selma) AL",
[702] = "La Crosse-Eau Claire WI",
[705] = "Wausau-Rhinelander WI",
[709] = "Tyler-Longview(Lufkin & Nacogdoches) TX",
[710] = "Hattiesburg-Laurel MS",
[711] = "Meridian MS",
[716] = "Baton Rouge LA",
[717] = "Quincy IL-Hannibal MO-Keokuk IA",
[718] = "Jackson MS",
[722] = "Lincoln & Hastings-Kearney NE",
[724] = "Fargo-Valley City ND",
[725] = "Sioux Falls(Mitchell) SD",
[734] = "Jonesboro AR",
[736] = "Bowling Green KY",
[737] = "Mankato MN",
[740] = "North Platte NE",
[743] = "Anchorage AK",
[744] = "Honolulu HI",
[745] = "Fairbanks AK",
[746] = "Biloxi-Gulfport MS",
[747] = "Juneau AK",
[749] = "Laredo TX",
[751] = "Denver CO",
[752] = "Colorado Springs-Pueblo CO",
[753] = "Phoenix AZ",
[754] = "Butte-Bozeman MT",
[755] = "Great Falls MT",
[756] = "Billings MT",
[757] = "Boise ID",
[758] = "Idaho Falls-Pocatello ID",
[759] = "Cheyenne WY-Scottsbluff NE",
[760] = "Twin Falls ID",
[762] = "Missoula MT",
[764] = "Rapid City SD",
[765] = "El Paso TX",
[766] = "Helena MT",
[767] = "Casper-Riverton WY",
[770] = "Salt Lake City UT",
[771] = "Yuma AZ-El Centro CA",
[773] = "Grand Junction-Montrose CO",
[789] = "Tucson (Sierra Vista) AZ",
[790] = "Albuquerque-Santa Fe NM",
[798] = "Glendive MT",
[800] = "Bakersfield CA",
[801] = "Eugene OR",
[802] = "Eureka CA",
[803] = "Los Angeles CA",
[804] = "Palm Springs CA",
[807] = "San Francisco-Oakland-San Jose CA",
[810] = "Yakima-Pasco-Richland-Kennewick WA",
[811] = "Reno NV",
[813] = "Medford-Klamath Falls OR",
[819] = "Seattle-Tacoma WA",
[820] = "Portland OR",
[821] = "Bend OR",
[825] = "San Diego CA",
[828] = "Monterey-Salinas CA",
[839] = "Las Vegas NV",
[855] = "Santa Barbara-Santa Maria-San Luis Obispo CA",
[862] = "Sacramento-Stockton-Modesto CA",
[866] = "Fresno-Visalia CA",
[868] = "Chico-Redding CA",
[881] = "Spokane WA",
}
local function Dump(p)
if type(p) == 'table' then
local a = {}
for k in pairs(p) do
table.insert(a, k)
end
table.sort(a)
Write('<dl>\n')
for i = 1,#a do
local k = a[i]
local v = p[k]
Write('<dt>')
Dump(k)
Write('\n')
Write('<dd>')
if k == 'iso_code' then
Write('<img style="max-width:32px" src="//justine.lol/flags/' .. v:lower() .. '.png"> ')
end
Dump(v)
if k == 'metro_code' then
local metro = kMetroCodes[v]
if metro then
Write(' (' .. metro .. ')')
end
elseif k == 'accuracy_radius' then
Write(' km')
end
Write('\n')
end
Write('</dl>\n')
else
Write(EscapeHtml(tostring(p)))
end
end
local function main()
if GetMethod() ~= 'GET' and GetMethod() ~= 'HEAD' then
ServeError(405)
SetHeader('Allow', 'GET, HEAD')
return
end
local ip = nil
local geo = nil
local asn = nil
local value = ''
if HasParam('ip') then
local geodb = maxmind.open('/usr/local/share/maxmind/GeoLite2-City.mmdb')
local asndb = maxmind.open('/usr/local/share/maxmind/GeoLite2-ASN.mmdb')
ip = ParseIp(GetParam('ip'))
if ip ~= -1 then
value = FormatIp(ip)
geo = geodb:lookup(ip)
if geo then
geo = geo:get()
end
asn = asndb:lookup(ip)
if asn then
asn = asn:get()
end
if not geo and not asn then
SetStatus(404)
end
end
end
SetHeader('Content-Type', 'text/html; charset=utf-8')
Write('<!doctype html>\n')
Write([[
<title>maxmind redbean demo</title>
<form action="maxmind.lua" method="get">
<input type="text" id="ip" name="ip" placeholder="8.8.8.8"
value="]] .. value .. [[">
<label for="ip">ip address</label>
<br>
<input type="submit" value="Lookup">
</form>
]])
if ip then
Write('<h3>Maxmind Geolite DB</h3>')
if geo then
Dump(geo)
else
Write('<p>Not found\n')
end
Write('<h3>Maxmind ASN DB</h3>')
if asn then
Dump(asn)
else
Write('<p>Not found\n')
end
end
end
main()

View file

@ -1164,7 +1164,7 @@ MAXMIND MODULE
Write(EscapeHtml(asorg))
end
For further details, please see tool/net/lmaxmind.c
For further details, please see maxmind.lua in redbean-demo.com.
CONSTANTS

View file

@ -117,6 +117,7 @@ o/$(MODE)/tool/net/demo/.reload.lua.zip.o \
o/$(MODE)/tool/net/demo/sql.lua.zip.o \
o/$(MODE)/tool/net/demo/fetch.lua.zip.o \
o/$(MODE)/tool/net/demo/hello.lua.zip.o \
o/$(MODE)/tool/net/demo/maxmind.lua.zip.o \
o/$(MODE)/tool/net/demo/redbean.lua.zip.o \
o/$(MODE)/tool/net/demo/opensource.lua.zip.o \
o/$(MODE)/tool/net/demo/closedsource.lua.zip.o \
@ -154,6 +155,7 @@ o/$(MODE)/tool/net/redbean-demo.com.dbg: \
o/$(MODE)/tool/net/demo/fetch.lua.zip.o \
o/$(MODE)/tool/net/demo/hello.lua.zip.o \
o/$(MODE)/tool/net/demo/redbean.lua.zip.o \
o/$(MODE)/tool/net/demo/maxmind.lua.zip.o \
o/$(MODE)/tool/net/demo/opensource.lua.zip.o \
o/$(MODE)/tool/net/demo/closedsource.lua.zip.o \
o/$(MODE)/tool/net/demo/printpayload.lua.zip.o \

View file

@ -70,12 +70,15 @@ void PrintImage(int yn, int xn, unsigned char rgb[yn][xn][3]) {
if (want24bit_) {
printf("\033[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄",
rgb[y + 0][x][0], rgb[y + 0][x][1], rgb[y + 0][x][2],
rgb[y + 1][x][0], rgb[y + 1][x][1], rgb[y + 1][x][2]);
rgb[MIN(y + 1, yn - 1)][x][0], rgb[MIN(y + 1, yn - 1)][x][1],
rgb[MIN(y + 1, yn - 1)][x][2]);
} else {
printf(
"\033[48;5;%hhu;38;5;%hhum▄",
rgb2xterm256(rgb[y + 0][x][0], rgb[y + 0][x][1], rgb[y + 0][x][2]),
rgb2xterm256(rgb[y + 1][x][0], rgb[y + 1][x][1], rgb[y + 1][x][2]));
rgb2xterm256(rgb[MIN(y + 1, yn - 1)][x][0],
rgb[MIN(y + 1, yn - 1)][x][1],
rgb[MIN(y + 1, yn - 1)][x][2]));
}
}
printf("\e[0m\n");

View file

@ -325,19 +325,24 @@ static void *DeblinterlaceSubpixelBgr(long dyn, long dxn,
}
static void PrintImageSerious(long yn, long xn, unsigned char RGB[3][yn][xn],
struct TtyRgb TTY[yn][xn], char *vt) {
long tyn, long txn, struct TtyRgb TTY[tyn][txn],
char *vt) {
char *p;
long y, x;
struct TtyRgb bg = {0x12, 0x34, 0x56, 0};
struct TtyRgb fg = {0x12, 0x34, 0x56, 0};
if (g_flags.unsharp) unsharp(3, yn, xn, RGB, yn, xn);
if (g_flags.dither) dither(yn, xn, RGB, yn, xn);
for (y = 0; y < yn; ++y) {
for (x = 0; x < xn; ++x) {
TTY[y][x] = rgb2tty(RGB[0][y][x], RGB[1][y][x], RGB[2][y][x]);
if (yn && xn) {
for (y = 0; y < tyn; ++y) {
for (x = 0; x < txn; ++x) {
TTY[y][x] = rgb2tty(RGB[0][MIN(y, yn - 1)][MIN(x, xn - 1)],
RGB[1][MIN(y, yn - 1)][MIN(x, xn - 1)],
RGB[2][MIN(y, yn - 1)][MIN(x, xn - 1)]);
}
}
}
p = ttyraster(vt, (void *)TTY, yn, xn, bg, fg);
p = ttyraster(vt, (void *)TTY, tyn, txn, bg, fg);
*p++ = '\r';
if (g_flags.trailingnewline) *p++ = '\n';
p = stpcpy(p, "\e[0m");
@ -345,6 +350,7 @@ static void PrintImageSerious(long yn, long xn, unsigned char RGB[3][yn][xn],
}
static void ProcessImage(long yn, long xn, unsigned char RGB[3][yn][xn]) {
long tyn, txn;
if (g_flags.half) {
if (g_flags.subpixel) {
PrintImageLR(yn, xn * 3, RGB, 0, yn, 0, xn * 3);
@ -352,10 +358,12 @@ static void ProcessImage(long yn, long xn, unsigned char RGB[3][yn][xn]) {
PrintImage(yn, xn, RGB, 0, yn, 0, xn);
}
} else {
tyn = ROUNDUP(yn, 2);
txn = ROUNDUP(xn, 2);
PrintImageSerious(
yn, xn, RGB, gc(memalign(32, sizeof(struct TtyRgb) * yn * xn)),
gc(memalign(32, ((yn * xn * strlen("\e[48;2;255;48;2;255m▄")) +
(yn * strlen("\e[0m\r\n")) + 128))));
yn, xn, RGB, tyn, txn, gc(calloc(sizeof(struct TtyRgb), tyn * txn)),
gc(calloc(1, ((yn * xn * strlen("\e[48;2;255;48;2;255m▄")) +
(yn * strlen("\e[0m\r\n")) + 128))));
}
}
@ -400,6 +408,7 @@ void WithImageFile(const char *path,
int main(int argc, char *argv[]) {
int i;
ShowCrashReports();
GetOpts(&argc, argv);
stbi_set_unpremultiply_on_load(true);
for (i = optind; i < argc; ++i) {